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

注意:rand()返回的值范圍為[0 , 215),當需要生成的數大於這個范圍時建議使用( rand() << 15 | rand() )

#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);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM