- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
~Nafis
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
~Nafis
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
~Nafis
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 (http://www.intel.com/software/products/mkl/docs/vslnotes.pdf).
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page