メインの.cppブースト::ランダム
#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、...
使用方法ブースト:ランダム正しくですか?
それです。それはそれはdoesnのように、あなたはあなたの乱数ジェネレータをシードする必要が同じ乱数
int get_random() {get_seed();return generate_random();} for (;;) {cout << get_random() <<endl;} // output the same random number yet
雅右。シードが同じ場合、ジェネレータは同じ乱数で始まり、毎回 – A9S6
となります。次のように少し問題があるかもしれません: それは音のようです get_seed(); for(;;){cout << generate_random()<< endl; } // OKです。 同じ乱数を生成します。 int get_random(){get_seed();}();} // get_random()<< endl; //出力同じ乱数はまだ – mono
は、関数が呼び出されるたびにシードを生成します。同じ種子を使用する毎回同じ種子を使用する ? なぜ私は前の方法で同じ乱数シーケンスを取得する – mono