C語言區間隨機數產生 with srand() & rand() & time()

來源:互聯網
上載者:User

在用電腦的一些智能演算法(GA,PSO,ANN etc.)模擬時經常需要隨機產生初始種群(初始樣本),看看<stdlib.h>中的這兩個函數的偽隨機數產生吧~~~

1. 產生[a,b]之間的一個實數和一個整數 [cpp] view plaincopy
  1. /* 
  2. 定義函數     int   rand(void); 
  3. 函數說明     rand()會返回一隨機數值,範圍在0至RAND_MAX   間。 
  4. 在調用此函數產生隨機數前,必須先利用srand()設好隨機數種子,如果未設隨機數種子,rand()在調用時會自動設隨機數種子為1。 
  5. 關於隨機數種子請參考srand()。  
  6. 傳回值     返回0至RAND_MAX之間的隨機數值,RAND_MAX定義在stdlib.h,其值為2147483647。  
  7. 範例 : 
  8. */  
  9. #include <stdlib.h>   
  10. #include <stdio.h>  
  11.   
  12. double  doubleRand(double a,double b); 
  13. int         intRand(int a,int b);  
  14.   
  15. int main(void)  
  16. {  
  17.     double i=doubleRand(2.0,9.0);  
  18.     int     j=intRand(2,9);  
  19.     printf("%f \n",i);  
  20.     printf("%d \n",j );  
  21.   
  22.     return 0;  
  23. }   
  24. double doubleRand(double a,double b)  
  25. {  
  26.     double r;  
  27.     r=(double)rand()/RAND_MAX;  
  28.     return a+r*(b-a);  
  29. }  
  30. int intRand(int a,int b)  
  31. {  
  32.     return (int)doubleRand(a,b);  
  33. }  
以上代碼中的每個函數只能產生一個隨機數,至於為什麼呢?2.產生多個隨機數

       之所以rand()每次的隨機數都一樣是因為rand()函數使用不正確。各種程式設計語言返回的隨機數(確切地說是偽隨機數)實際上都是根據遞推公式計算的一組數值,當序列足夠長,這組數值近似滿足均勻分布。如果計算偽隨機序列的初始數值(稱為種子)相同,則計算出來的偽隨機序列就是完全相同的。這個特性被有的軟體利用於加密和解密。加密時,可以用某個種子數產生一個偽隨機序列並對資料進行處理;解密時,再利用種子數產生一個偽隨機序列並對加密資料進行還原。這樣,對於不知道種子數的人要想解密就需要多費些事了。當然,這種完全相同的序列對於你來說是非常糟糕的。要解決這個問題,需要在每次產生隨機序列前,先指定不同的種子,這樣計算出來的隨機序列就不會完全相同了。你可以在調用rand()函數之前調用srand(   (unsigned)time(NULL)),這樣以time函數值(即目前時間)作為種子數,因為兩次調用rand函數的時間通常是不同的,這樣就可以保證隨機性了。你也可以使用srand函數來人為指定種子數。

好,那按照這樣,我就這樣寫~~~

[cpp] view plaincopy

 
  1. #include   <stdlib.h>     
  2. #include   <stdio.h>     
  3. #include   <time.h>     
  4.   
  5. int main()     
  6. {     
  7.     for(int   i=0;i <100000;i++)     
  8.     {     
  9.         srand(   (unsigned)time(   NULL   )   );     
  10.         printf("%d\n",rand() );  
  11.     }    
  12.     return 0;  
  13. }    
答:你的程式是有問題的,你每產生一個隨機數之前,都調用一次srand,而由於電腦運行很快,所以你每次用time得到的時間都是一樣的(time的時間精度較低,只有55ms)。這樣相當於使用同一個種子產生隨機序列,所以產生的隨機數總是相同的。 你應該把srand放在迴圈外: [cpp] view plaincopy
  1. /* 
  2. #include <stdlib.h> 
  3. void srand(unsigned seed);  
  4. #include <time.h> 
  5. time_t  time(time_t *time);
  6. */  
  7. #include   <stdlib.h>     
  8. #include   <stdio.h>     
  9. #include   <time.h>     
  10.   
  11. int main()     
  12. {     
  13.     int i;  
  14.      //以當前系統時間作為種子  
  15.     srand(   (unsigned)time(   NULL   )   );     
  16.     for(i=0;i <10;i++)     
  17.     {     
  18.         printf("%d\n",rand() );  
  19.     }    
  20.     return 0;  
  21. }    
3. 若要不重複呢?即種群中的粒子都是不同的~~~先來個最笨的辦法:就是我拿一個數組來存你產生的隨機數,一個一個放進來,邊放邊檢查,這樣的複雜度隨著個數成階層增長~~~且時間是不可預測的,這對RTOS是不好的訊息~~~但是簡單好實現,走一個先~~~ [cpp] view plaincopy
  1. #include   <stdlib.h>     
  2. #include   <stdio.h>     
  3. #include   <time.h>     
  4. #define MAX_NUM 10000  
  5. /*when insert a Rand_Num then check it*/  
  6. int check(int a[],int i)  
  7. {  
  8.     int j;  
  9.     for(j=0;j<i;j++)  
  10.         if(*(a+j)==*(a+i))  
  11.             return 0;  
  12.     return 1;  
  13. }  
  14. int main()     
  15. {     
  16.     int i;  
  17.     int a[MAX_NUM];  
  18.      //以當前系統時間作為種子  
  19.     srand(   (unsigned)time(   NULL   )   );     
  20.     for(i=0;i <10;i++)     
  21.     {     
  22.         a[i]=rand();  
  23.         if (check(a,i)==0)  
  24.         {  
  25.             i--;    
  26.             continue;      //the number is the same with of one of the array number,so once again  
  27.         }  
  28.         printf("%d\n",a[i] );  
  29.     }    
  30.     return 0;  
  31. }    
這個和洗牌演算法很類似,但沒有那麼分牌規則那麼嚴格,應用的地方不同~~~ 話說其實,種群產生的粒子很多情況下可以可以重複(與具體問題模型有關)~~~那麼不笨的方法呢?這個不像洗牌演算法那麼多規則,再想想~~~好了,至少PSO的粒子可以產生了~~~:)源文地址:http://www.software8.co/wzjs/cpp/1043.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.