to check c++ code, able let boost::random , matlab produce same random numbers.
so boost use code:
boost::mt19937 var(static_cast<unsigned> (std::time(0))); boost::uniform_int<> dist(1, 6); boost::variate_generator<boost::mt19937&, boost::uniform_int<> > die(var, dist); die.engine().seed(0); for(int = 0; < 10; ++i) { std::cout << die() << " "; } std::cout << std::endl;
which produces (every run of program):
4 4 5 6 4 6 4 6 3 4
and matlab use:
randstream.setdefaultstream(randstream('mt19937ar','seed',0)); randi(6,1,10)
which produces (every run of program):
5 6 1 6 4 1 2 4 6 6
which bizarre, since both use same algorithm, , same seed. miss?
it seems python (using numpy) , matlab seems comparable, in random uniform numbers: matlab
randstream.setdefaultstream(randstream('mt19937ar','seed',203));rand(1,10)
0.8479 0.1889 0.4506 0.6253 0.9697 0.2078 0.5944 0.9115 0.2457 0.7743
python: random.seed(203);random.random(10)
array([ 0.84790006, 0.18893843, 0.45060688, 0.62534723, 0.96974765, 0.20780668, 0.59444858, 0.91145688, 0.24568615, 0.77430378])
c++boost
0.8479 0.667228 0.188938 0.715892 0.450607 0.0790326 0.625347 0.972369 0.969748 0.858771
which identical ever other python , matlab value...
i have agree other answers, stating these generators not "absolute". may produce different results according implementation. think simplest solution implement own generator. might daunting (mersenne twister sure way) take @ xorshift, extremely simple though powerful one. copy c implementation given in wikipedia link :
uint32_t xor128(void) { static uint32_t x = 123456789; static uint32_t y = 362436069; static uint32_t z = 521288629; static uint32_t w = 88675123; uint32_t t; t = x ^ (x << 11); x = y; y = z; z = w; return w = w ^ (w >> 19) ^ (t ^ (t >> 8)); }
to have same seed, put values want int x,y,z,w (except(0,0,0,0) believe). need sure matlab , c++ use both 32 bit these unsigned int.
Comments
Post a Comment