主要的.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
雅權。如果種子是相同的,那麼生成器將每次以相同的隨機數開始 – A9S6 2009-12-04 07:50:02
就是這樣。但也許有一點問題,如下所示: 似乎聽起來 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
會生成一個種子。 vs.每次使用相同的種子 這是相同的種子? 爲什麼我在 – mono 2009-12-09 14:34:32