C语言中rand()函数及time库相关

rand()函数

rand() 函数原型是int rand(void),它会返回一个从0到RAND_MAX的整数(RAND_MAX是 C 语言标准库 <stdlib.h> 中定义的一个宏 0x7fff 即32767)

但是仅仅用rand()返回的结果是不变的,因为rand()函数是根据一个数(我们称之为种子)通过固定的公式计算而来的,但是计算机开机后,这个种子的值是定了的,所以结果不变

C提供了 srand()函数,它的原型是void srand( int a) 功能是初始化rand()函数的初始值

但是对于种子a,我们不可能每产生一次随机数,就手动输入一次a吧

所以我们就以计算机的时间作为种子,那么种子就会自己变了( time()函数见下方讲解 )

#include <bits/stdc++.h>
using namespace std;
int main()
{
    srand(time(NULL)); //time(NULL)即为获取系统时间
    cout << rand() << endl;
    return 0;
}

那如何生成一定范围 内的随机数呢,很容易想到用“%”和“+”即可实现

因为  0 <= rand()%(n-m+1) <= n-m

所以 m <= rand()%(n-m+1)+m <= n

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, m;
    cin >> m >> n;
    srand(time(NULL));
    cout << rand() % (n - m + 1) + m << endl;  //生成[m,n]之间的随机整数
    return 0;
}

time.h库相关

time.h 头文件定义了四个变量类型、两个宏和各种操作日期和时间的函数。

四个变量

size_t 是无符号整数类型,它是 sizeof 关键字的结果
clock_t 这是一个适合存储处理器时间的类型,类型为unsigned long
time_t 这是一个适合存储日历时间类型
struct tm 这是一个用来保存时间和日期的结构

tm结构体定义如下:

struct tm
{
  int tm_sec;/* seconds - [0,59] */
  int tm_min;/* minutes - [0,59] */
  int tm_hour;/* hours - [0,23] */
  int tm_mday;/* day of the month - [1,31] */
  int tm_mon;/* months since January - [0,11] */
  int tm_year;/* years since 1900 */
  int tm_wday;/* days since Sunday - [0,6] */
  int tm_yday;/* days since January 1 - [0,365] */
  int tm_isdst;/* daylight savings time flag */
};

两个宏

NULL 这个宏是一个空指针常量的值
CLOCKS_PER_SEC

表示一秒钟内CPU运行的时钟周期数

用于将clock()函数的结果转化为以秒为单位的量

但是这个量的具体值是与操作系统相关的,通常为1000

部分库函数

difftime() : 以秒数计算二个作为 time_t 对象的日历时间的差(time_end - time_beg),若 time_end 代表先于 time_beg 的时间点,则结果为负。函数原型:double difftime(time_t time2, time_t time1)

mktime() : 函数原型:time_t mktime(struct tm *timeptr),把 timeptr 所指向的结构转换为一个依据本地时区的 time_t 值

time() : 返回编码成 time_t 对象的当前日历时间,并将其存储于 arg 指向的 time_t 对象(除非 arg 为空指针,timer=NULL时得到当前日历时间(从1970-01-01 00:00:00到现在的秒数),timer=时间数值时,用于设置日历时间,time_t是一个unsigned long类型。函数原型:time_t time(time_t *timer)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    time_t seconds;
    seconds = time(NULL);
    printf("Hours since 1970-01-01 = %ld\n", seconds / 3600);
    return 0;
}

clock() : 返回clock函数执行起(一般为程序的开头),处理器时钟所使用的时间,一般用来计算程序或程序的某一段的执行时间,函数原型:clock_t clock(void)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    clock_t start_time,finish_time;
    start_time = clock();

    // running    用这个就可以测试程序运行时间了

    finish_time = clock();
    double total_time = (double)(finish_time - start_time) / CLOCKS_PER_SEC;  //将时间转换为秒  若想要得到毫秒 把 CLOCKS_PER_SEC 删去即可
    printf("The time the program is running:%f\n", total_time);
    return 0;
}

asctime() : 将结构体tm所定义的变量表示的时间用字符串表示出来,函数原型:char* asctime(struct tm * ptr)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    struct tm t;
    t.tm_sec = 10;
    t.tm_min = 50;
    t.tm_hour = 16;
    t.tm_mday = 6;
    t.tm_mon = 7;
    t.tm_year = 119;
    t.tm_wday = 2;

    printf("%s\n", asctime(&t));//2019/8/6 16:50:10 星期二
    return (0);
}

 

localtime() : 函数原型:struct tm *localtimeconst time_t *time ),转换从纪元开始的给定时间( time 所指向的 time_t 的值),以 struct tm 格式及本地时间表达的日历时间

#include <bits/stdc++.h>
using namespace std;
int main()
{
    time_t timer;
    struct tm *Now;
    time(&timer);            //获取当前时间
    Now = localtime(&timer); //填充struct tm 结构
    printf("Current local time and date:%s", asctime(Now));
    return (0);
}

 ctime() : 将指定的从纪元开始时间转换成本地日历时间,再变成文本展示,如同调用 asctime(localtime(time)),函数原型:char *ctime(const time_t * timer)

#include <bits/stdc++.h>
using namespace std;
int main()
{

    time_t curtime;
    time(&curtime);
    printf("current time = %s", ctime(&curtime));
    return (0);
}

猜你喜欢

转载自www.cnblogs.com/Zeronera/p/11311000.html