在用電腦的一些智能演算法(GA,PSO,ANN etc.)模擬時經常需要隨機產生初始種群(初始樣本),看看<stdlib.h>中的這兩個函數的偽隨機數產生吧~~~
1. 產生[a,b]之間的一個實數和一個整數
[cpp] view plaincopy
- /*
- 定義函數 int rand(void);
- 函數說明 rand()會返回一隨機數值,範圍在0至RAND_MAX 間。
- 在調用此函數產生隨機數前,必須先利用srand()設好隨機數種子,如果未設隨機數種子,rand()在調用時會自動設隨機數種子為1。
- 關於隨機數種子請參考srand()。
- 傳回值 返回0至RAND_MAX之間的隨機數值,RAND_MAX定義在stdlib.h,其值為2147483647。
- 範例 :
- */
- #include <stdlib.h>
- #include <stdio.h>
-
- double doubleRand(double a,double b);
- int intRand(int a,int b);
-
- int main(void)
- {
- double i=doubleRand(2.0,9.0);
- int j=intRand(2,9);
- printf("%f \n",i);
- printf("%d \n",j );
-
- return 0;
- }
- double doubleRand(double a,double b)
- {
- double r;
- r=(double)rand()/RAND_MAX;
- return a+r*(b-a);
- }
- int intRand(int a,int b)
- {
- return (int)doubleRand(a,b);
- }
以上代碼中的每個函數只能產生一個隨機數,至於為什麼呢?2.產生多個隨機數
之所以rand()每次的隨機數都一樣是因為rand()函數使用不正確。各種程式設計語言返回的隨機數(確切地說是偽隨機數)實際上都是根據遞推公式計算的一組數值,當序列足夠長,這組數值近似滿足均勻分布。如果計算偽隨機序列的初始數值(稱為種子)相同,則計算出來的偽隨機序列就是完全相同的。這個特性被有的軟體利用於加密和解密。加密時,可以用某個種子數產生一個偽隨機序列並對資料進行處理;解密時,再利用種子數產生一個偽隨機序列並對加密資料進行還原。這樣,對於不知道種子數的人要想解密就需要多費些事了。當然,這種完全相同的序列對於你來說是非常糟糕的。要解決這個問題,需要在每次產生隨機序列前,先指定不同的種子,這樣計算出來的隨機序列就不會完全相同了。你可以在調用rand()函數之前調用srand( (unsigned)time(NULL)),這樣以time函數值(即目前時間)作為種子數,因為兩次調用rand函數的時間通常是不同的,這樣就可以保證隨機性了。你也可以使用srand函數來人為指定種子數。
好,那按照這樣,我就這樣寫~~~
[cpp] view plaincopy
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
-
- int main()
- {
- for(int i=0;i <100000;i++)
- {
- srand( (unsigned)time( NULL ) );
- printf("%d\n",rand() );
- }
- return 0;
- }
答:你的程式是有問題的,你每產生一個隨機數之前,都調用一次srand,而由於電腦運行很快,所以你每次用time得到的時間都是一樣的(time的時間精度較低,只有55ms)。這樣相當於使用同一個種子產生隨機序列,所以產生的隨機數總是相同的。 你應該把srand放在迴圈外:
[cpp] view plaincopy
- /*
- #include <stdlib.h>
- void srand(unsigned seed);
- #include <time.h>
- time_t time(time_t *time);
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
-
- int main()
- {
- int i;
- //以當前系統時間作為種子
- srand( (unsigned)time( NULL ) );
- for(i=0;i <10;i++)
- {
- printf("%d\n",rand() );
- }
- return 0;
- }
3. 若要不重複呢?即種群中的粒子都是不同的~~~先來個最笨的辦法:就是我拿一個數組來存你產生的隨機數,一個一個放進來,邊放邊檢查,這樣的複雜度隨著個數成階層增長~~~且時間是不可預測的,這對RTOS是不好的訊息~~~但是簡單好實現,走一個先~~~
[cpp] view plaincopy
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #define MAX_NUM 10000
- /*when insert a Rand_Num then check it*/
- int check(int a[],int i)
- {
- int j;
- for(j=0;j<i;j++)
- if(*(a+j)==*(a+i))
- return 0;
- return 1;
- }
- int main()
- {
- int i;
- int a[MAX_NUM];
- //以當前系統時間作為種子
- srand( (unsigned)time( NULL ) );
- for(i=0;i <10;i++)
- {
- a[i]=rand();
- if (check(a,i)==0)
- {
- i--;
- continue; //the number is the same with of one of the array number,so once again
- }
- printf("%d\n",a[i] );
- }
- return 0;
- }
這個和洗牌演算法很類似,但沒有那麼分牌規則那麼嚴格,應用的地方不同~~~ 話說其實,種群產生的粒子很多情況下可以可以重複(與具體問題模型有關)~~~那麼不笨的方法呢?這個不像洗牌演算法那麼多規則,再想想~~~好了,至少PSO的粒子可以產生了~~~:)源文地址:http://www.software8.co/wzjs/cpp/1043.html