Module prand
source code
Mersenne Twister based random number generator
This module is intended to provide a reasonable fast random number
generator object that allows for (1) multiple generators running in
parallel and (2) saving/restoring of generator state to allow
easy regeneration of number sequences and (3) use a welll documented
algorithm (Mersenne Twister) that can easily be implemented or
exists in Matlab.
Starting with Matlab 7.1 the built in RAND() function uses the
same Mersenne Twister engine that python's (2.4 on) random.Random()
object uses. This means you can go back and forth pretty easily.
The only catach is that python's state vector is composed of signed
long's, while matlab's is unsigned. This means that to take the
python state vector into matlab you need to add 2^32 to all negative
values in the state vector before passing to the RAND function:
>> s = [state vector from puthon..]
>> s(s<0)=s(s<0)+(2^32); rand('twister', s);
Will put the generator at the same state the python generator was
at when the state was saved. RAND(100) in matlab will now generate
the same random sequence the python call:
>> PypeRandom(seed=statevector).rand(100)
would. At least to about +- 1.0e-11 as far as I can tell..
Author -- Matt Krause (matthew.krause@yale.edu)
Valid Mersenne Twister engine.
Check to see if python is still using the expected Mersenne Twister
random number generator. This will almost certainly fail if anything
changes..
|