I tried std::mt19937 gen(2007)
in c++ and RandStream.create('mt19937ar','seed',2007)
in Matlab. I have also tried different structures, but I couldn't find a specific seed structure to find the same random numbers between Matlab and c++. How can I handle this?
Specify the Seed Every time you initialize the generator using the same seed, you always get the same result. First, initialize the random number generator to make the results in this example repeatable. rng('default'); Now, initialize the generator using a seed of 1 .
rng( seed ) specifies the seed for the MATLAB® random number generator. For example, rng(1) initializes the Mersenne Twister generator using a seed of 1 . The rng function controls the global stream, which determines how the rand , randi , randn , and randperm functions produce a sequence of random numbers.
You will notice that each time you start up a new MATLAB session, the random numbers returned by RAND are the same. This is because MATLAB's random number generator is initialized to the same state each time MATLAB starts up.
The seed() method is used to initialize the random number generator. The random number generator needs a number to start with (a seed value), to be able to generate a random number. By default the random number generator uses the current system time.
You want a random generator to be deterministic and work the same way in two distinct implementations.
There is no guarantee that Matlab and c++::std implementations will generate same results. Although it would be reasonable to think that they should - it is the same algorithm after all. According to Wikipedia there are flavors to the implementations. Most noteworthy is the difference between 32 and 64 bit implementation that produces different results.
To overcome this obstacle generate the numbers in one tool and then use the same sequence in the other. Or use your own algorithm - some ideas here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With