random
how to get equal matlab random numbers in octave - using twister(!) algorithm?
- terminology: https://en.wikipedia.org/wiki/Mersenne_twister#Pseudocode
- nice2know: http://stackoverflow.com/questions/13735096/python-vs-octave-random-generator
initial situation
- ruby and python using twister algo as default
- force twister algo in matlab and octave
octave
octave:1> rand('twister',0) octave:2> rand ans = 0.844421851525048
python
>>> import random >>> random.seed(0) >>> random.random() 0.8444218515250481
numpy
>>> import numpy >>> numpy.random.seed(0) >>> numpy.random.random() 0.5488135039273248
ruby
irb(main):001:0> prng = Random.new(0) => #<Random:0x00000001d469e8> irb(main):002:0> prng.rand() => 0.5488135039273248
matlab
>> rand('twister',0) >> rand ans = 0.548813503927325
how to get the right seed states in octave?
rand('twister',0) states=rand('state');
seems to be the wrong ones. but they fit to them in python! ;)
function ret = twister_seed(SEED=0) ret = uint32(zeros(625,1)); ret(1) = SEED; for N = 1:623 ## initialize_generator # bit-xor (right shift by 30 bits) uint64(1812433253)*uint64(bitxor(ret(N),bitshift(ret(N),-30)))+N; # has to be uint64, otherwise in 4th iteration hit maximum of uint32! ret(N+1) = uint32(bitand(ans,uint64(intmax('uint32')))); # untempered numbers endfor ret(end) = 1; endfunction
octave:1> rand('twister',twister_seed) # notice: default seed is 0 in the function octave:2> rand ans = 0.548813503927325 octave:3> rand ans = 0.715189366372419 octave:4> rand ans = 0.602763376071644
compare to matlab...
>> rand('twister',0) >> rand ans = 0.548813503927325 >> rand ans = 0.715189366372419 >> rand ans = 0.602763376071644
TODO
- Find the `seed` stuff in octave source