Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any same seed for Matlab Random and C++ Random?

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?

like image 766
taha Avatar asked Jul 23 '14 10:07

taha


People also ask

How do I get the same random number in MATLAB?

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 .

How is the random seed for MATLAB?

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.

Why does Rand give the same number?

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.

What is seed in random sampling?

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.


1 Answers

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.

like image 100
mrVoid Avatar answered Oct 03 '22 09:10

mrVoid