1. 程式人生 > >C/C++ 生成[X,Y]內的隨機數

C/C++ 生成[X,Y]內的隨機數

  在C語言中,rand()函式可以用來產生隨機數,但是這不是真真意義上的隨機數,是一個偽隨機數。是根據一個數,稱為種子,為基準以某個遞推公式推算出來的一系數,當這系列數很大的時候,就符合正態公佈,從而相當於產生了隨機數,但這不是真正的隨機數

    rand()會返回一隨機數值,範圍在0至RAND_MAX 間。返回0至RAND_MAX之間的隨機數值,RAND_MAX定義在stdlib.h

在呼叫此函式rand()產生隨機數前,必須先利用srand()設好隨機數種子,如果未設隨機數種子,rand()在呼叫時會自動設隨機數種子為1,這樣每次呼叫生成的隨機數都是一致的。

如何每次產生不一致的隨機數:

   srand((int)time(0));//用系統時間來做種子

rand()和srand()函式。這二個函式的工作過程如下:

   1) 首先給srand()提供一個種子,它是一個unsigned int型別,其取值範圍從0~65535;

   2) 然後呼叫rand(),它會根據提供給srand()的種子值返回一個隨機數(在0到32767之間)

   3) 根據需要多次呼叫rand(),從而不間斷地得到新的隨機數;

例子:

  1. #include<stdio.h>

  2. #include<stdlib.h>

  3. #include<time.h>

  4. void main()

  5. {

  6. // srand((int)time(0));

  7. int x;

  8. int j;

  9. for(x=0;x<10;x++)

  10. printf("%d\t",rand_X(100));//輸出100內的隨機數

  11. }

  12. int rand_X(int x)

  13. {

  14. return rand()%x;

  15. }

第1次執行結果:

41      67      34      0       69      24      78      58      62      64

第2次執行結果:

41      67      34      0       69      24      78      58      62      64

......

第n次執行結果:

41      67      34      0       69      24      78      58      62      64

每次執行產生不一樣的隨機數:使用系統時間作為種子

例子:

  1. #include<stdio.h>

  2. #include<stdlib.h>

  3. #include<time.h>

  4. void main()

  5. {

  6. srand((int)time(0));//使用系統時間作為隨機種子

  7. int x;

  8. int j;

  9. for(x=0;x<10;x++)

  10. printf("%d\t",rand_X(100));//輸出100內的隨機數

  11. }

  12. int rand_X(int x)

  13. {

  14. return rand()%x;

  15. }

第1次執行:

84      9       54      43      84      24      48      39      71      54

第2次執行:

88      10      51      37      70      7       43      2       54      24

......

第n次執行:

32      87      43      47      59      43      18      74      67      53

如何產生設定範圍內的隨機數  

  1. 要讓隨機數限定在一個範圍,可以採用模除加法的方式。

  2. 要產生隨機數r, 其範圍為 X<=r<=Y,可以使用如下公式:

  3. rand()%(Y-X+1)+X

  4. 其原理為,對於任意數,

  5. 0<=rand()%(Y-X+1)<=Y-X

  6. 於是

  7. 0+X<=rand()%(Y-X+1)+X<=Y-X+X

  8. X<=rand()%(Y-X+1)+X<=Y

程式碼驗證:

  1. /**

  2. 生成[X,Y]的隨機數

  3. //rand()%(Y-X+1)+X

  4. */

  5. #include<stdio.h>

  6. #include<stdlib.h>

  7. #include<time.h>

  8. void main()

  9. {

  10. // int flags[11];//標誌位

  11. int i=0;

  12. srand((int)time(0));//用系統時間作為隨機種子

  13. int temp=-1;

  14. int Y=10;

  15. int X=0;

  16. //驗證確實生成了[0,10]的隨機數

  17. while(i<=10)

  18. {

  19. temp=rand()%(Y-X+1)+X;//生成[0,10]的隨機數

  20. if(temp>10||temp<0)

  21. {

  22. printf("錯誤!\n");

  23. break;

  24. }

  25. if(i==temp)

  26. {

  27. printf("temp=%d\n",temp);

  28. i++;

  29. }

  30. }

  31. }

結果:

  1. temp=0

  2. temp=1

  3. temp=2

  4. temp=3

  5. temp=4

  6. temp=5

  7. temp=6

  8. temp=7

  9. temp=8

  10. temp=9

  11. temp=10

  12. Process returned 8 (0x8) execution time : 0.250 s

  13. Press any key to continue.

可以看到並沒有輸出錯誤:可見確實生成了[0,10]的隨機整數
 

C++實現:

這裡和C語言沒什麼區別,就是輸入輸出由printf,scanf,改成cout,cin而已。以及對應都標頭檔案上多加了一個c:#include<ctime>,#includ<cstdlib>

  1. /**

  2. C++中獲取隨機數[x,y]

  3. */

  4. #include<iostream>

  5. #include<ctime>

  6. #include<cstdlib>

  7. using namespace std;

  8. int main()

  9. {

  10. srand((unsigned)time(NULL));

  11. // srand((int)time(0));//這兩句結果都是一樣的

  12. int Y=10;

  13. int X=0;

  14. int temps[11];

  15. int i=0;

  16. int temp;

  17. while(i<=10)

  18. {

  19. temp=rand()%(Y-X+1)+X;

  20. if(temp>10||temp<0)

  21. {

  22. cout<<"錯誤!"<<endl;

  23. }

  24. if(temp==i)

  25. {

  26. temps[i]=temp;

  27. i++;

  28. }

  29. }

  30. cout<<"i="<<i<<endl;

  31. for(i=0;i<=10;i++)

  32. {

  33. cout<<temps[i]<<" ";

  34. }

  35. return 0;

  36. }

結果:

  1. i=11

  2. 0 1 2 3 4 5 6 7 8 9 10

  3. Process returned 0 (0x0) execution time : 0.664 s

  4. Press any key to continue.