2009-12-04 72 views
10

主要的.cpp的boost ::隨機產生相同數量的每一次

#include  "stdafx.h" 
#include  "random_generator.h" 


     int 
main (int argc, char *argv[]) 
{ 
     cout.setf(ios::fixed); 
     base_generator_type base_generator; 
     int max = pow(10, 2); 
     distribution_type dist(1, max); 

     boost::variate_generator<base_generator_type&, 
distribution_type > uni(base_generator, dist); 
     for (int i=0; i<10; i++) { 
       //cout << random_number(2) << endl; 
       cout << uni() << endl; 
     } 

     return EXIT_SUCCESS; 

}        /* ---------- end of function main ---------- */ 

random_gemerator.h

#include  "stdafx.h" 

#include  <boost/random.hpp> 
#include  <boost/generator_iterator.hpp> 

typedef boost::mt19937 base_generator_type; 
typedef boost::lagged_fibonacci19937 fibo_generator_type; 
typedef boost::uniform_int<> distribution_type; 
typedef boost::variate_generator<fibo_generator_type&, 
distribution_type> gen_type; 

     int 
random_number (int bits) 
{ 
     fibo_generator_type fibo_generator; 
     int max = pow(10, bits); 
     distribution_type dist(1, max); 

     gen_type uni(fibo_generator, dist); 
     return uni(); 

}    /* ----- end of function random_number ----- */ 

的stdafx.h

#include <iostream> 
#include <cstdlib> 
#include <cmath> 

using namespace std; 

我每次運行它的時候,它全部生成相同的數字序列

喜歡77,33,5,22,...

如何使用boost:random是否正確?


就是這樣。但也許有一點問題,如下所示:

似乎聲音

get_seed(); for (;;) {cout << generate_random() << endl; } // is ok 

它genereate相同的隨機數

int get_random() {get_seed();return generate_random();} for (;;) {cout << get_random() <<endl;} // output the same random number yet 

回答

6

您需要種子的隨機數發生器所以它不」每次都從同一個地方開始。

根據您對數字的處理方式,您可能需要考慮如何選擇種子值。如果您需要高質量的隨機性(如果您生成加密密鑰並希望它們相當安全),則需要良好的種子值。如果這是Posix,我會建議/ dev/random - 但是你期望使用Windows,所以我不確定什麼是好的種子源。但是如果你不介意可預測的種子(用於遊戲,模擬等),快速和骯髒的種子是由time()返回的當前時間戳。

+0

雅權。如果種子是相同的,那麼生成器將每次以相同的隨機數開始 – A9S6 2009-12-04 07:50:02

+0

就是這樣。但也許有一點問題,如下所示: 似乎聽起來 get_seed(); for(;;){cout << generate_random()<< endl; } //很好 它生成相同的隨機數 int get_random(){get_seed(); return generate_random();} for(;;){cout << get_random()<< endl;} //輸出每次調用函數時,相同的隨機數 – mono 2009-12-09 08:15:58

+0

會生成一個種子。 vs.每次使用相同的種子 這是相同的種子? 爲什麼我在 – mono 2009-12-09 14:34:32

13

,如果你想的隨機數序列改變每次運行程序的時候,你需要與當前時間初始化它例如

改變隨機種子,你會發現一個例子there,摘錄:

/* 
* Change seed to something else. 
* 
* Caveat: std::time(0) is not a very good truly-random seed. When 
* called in rapid succession, it could return the same values, and 
* thus the same random number sequences could ensue. If not the same 
* values are returned, the values differ only slightly in the 
* lowest bits. A linear congruential generator with a small factor 
* wrapped in a uniform_smallint (see experiment) will produce the same 
* values for the first few iterations. This is because uniform_smallint 
* takes only the highest bits of the generator, and the generator itself 
* needs a few iterations to spread the initial entropy from the lowest bits 
* to the whole state. 
*/ 
generator.seed(static_cast<unsigned int>(std::time(0))); 
+0

就是這樣。但也許有一點問題,如下所示: 似乎聽起來 get_seed(); for(;;){cout << generate_random()<< endl; } //很好 它生成相同的隨機數 int get_random(){get_seed(); return generate_random();} for(;;){cout << get_random()<< endl;} //輸出相同的隨機數尚未 – mono 2009-12-09 08:16:38

+8

我喜歡初始化PRNG來'的std ::時間(NULL)+ GETPID()'。這樣可以確保二進制文件一個接一個地執行,並且PRNG的初始化方式不同。 – user1202136 2012-04-03 07:21:39

+1

爲了增加「安全」,正確的方法應該是使用初始化PRNG的/ dev /隨機(或[視窗]下的等效(https://en.wikipedia.org/wiki/Entropy_(計算)#Windows)。 ) – Avio 2016-06-08 15:59:18

5

如果你是一個「nix的系統上運行,你總是可以嘗試這樣的事情;

int getSeed() 
{ 
    ifstream rand("/dev/urandom"); 
    char tmp[sizeof(int)]; 
    rand.read(tmp,sizeof(int)); 
    rand.close(); 
    int* number = reinterpret_cast<int*>(tmp); 
    return (*number); 
} 

我猜播種隨機數生成器這種方式比簡單地讀取/dev/urandom(或/dev/random)爲您的所有隨機數需要更快。

+0

這是正確的想法。請注意,至少在Boost的新版本中,這些都是由'random_device'抽象出來的。作爲獎勵,'random_device'也應該在Windows上實現。不幸的是,如果我理解正確,它不在BSD上實現,因爲BSD/dev/urandom並不是非確定性的。所以,如果你想Linux和Windows的互操作性,我想你可以使用'random_device',如果你想要Linux和BSD的互操作性,你可以使用/ dev/urandom的顯式讀取。如果你想要一些其他的互操作性組合,我認爲你是獨立的! – Chinasaur 2011-09-22 22:16:24

2

您可以按原樣使用boost::random::random_device類,也可以爲其他生成器播種。

你可以得到一個一次性隨機數出它有一個簡單的:

 
boost::random::random_device()() 

+0

看起來好像它返回一個類型'無符號int'的結果(我想知道......所以不得不來關注一下吧)。 – 2013-10-16 00:18:52

相關問題