Using VSL to generate uniformly distributed random doubles

Using VSL to generate uniformly distributed random doubles

I have the following code:

int nrand = 1;

double *x = new double[nrand];

double *y = new double[nrand];

VSLStreamStatePtr stream1;

vslNewStream(&stream1, VSL_BRNG_MCG31, static_cast<unsigned>(time(NULL)));

vdRngUniform(VSL_METHOD_DUNIFORM_STD, stream1, nrand, x, 0, 1);

VSLStreamStatePtr stream2;

vslNewStream(&stream2, VSL_BRNG_MCG31, static_cast<unsigned>(time(NULL)));

vdRngUniform(VSL_METHOD_DUNIFORM_STD, stream2, nrand, y, 0, 1);

I keep getting the same generated values for xand yevery time. What am I doing wrong?


5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Thank you for your question. In both cases you initialize random streams with the same seed and obtain identical outputs.You may check this printing out the values returned by the time() function. If you suspend your program for at least 1 sec you will be able toobserve difference in outputs of the generator.

Right, but the same values are always generated from run to run (i.e.I keep getting the same value for x every time).Why is that?


Yes, there are different ways of choosing the seed that produce better results, but what's wrong with simply using time(NULL)? Does the particular BRNG I'm using not produce varying results for very large unsigned seed values?


Time which passes between creation/initialization of stream1 and stream2is expected to be much less than 1 sec. So,two calls to time() routine in the program return the same number of seconds, both streamsare initialized with the same seed and their outputs are identical from run to run.If you stop your program before creation of the second stream for 1 second, seed used for initialization of stream2 will be another, and its output willdifferent. Also, to make sure that you obtain new number x from run to run of your program you need to print moredigits after the point.

Initialization of MCG31m1 generator is made in the following way: x0 = seed mod 2^31-1 (if x0=0 thenx0 is set to 1). So, if value which is returned by the function time() is greater than 2^31-1corresponding remainder will be chosen for initialization of the first element of random sequence.Currently,output of thefunction time() isless than 2^31-1, andnumber of seconds returned by the function is directly usedin calculation of the first random number. More detailed information about initialization of MCG31m1can be found in Vslnotes.pdf (

Leave a Comment

Please sign in to add a comment. Not a member? Join today