PRNG class not discarding properly?

PRNG class not discarding properly?

by ByungChan Ha -
Number of replies: 2

For assignment 9, I was trying to make sure that the discarding function worked properly for the PRNG class that was provided to us. When I ran a couple of tests by modifying the number of skips and generating more numbers in prng_example.cpp, the generated random numbers were not what I was expecting...Could I ask if the numbers generated below is what I'm supposed to see when I use the discarding function?

To be more precise, my tests generated total of six numbers with some "X" number of skips (achieved by calling randomDouble(X)) between the first three and the last three. The results were....(I did not touch any of the provided functions, retaining the default seed of 13)

Even X's:

X = 0, 2 (both somehow give the same results)

0.105422, 0.590947, 0.465967, Skipping the next 0 (or 2) numbers... 0.665662, 0.285069, 0.524366

X = 4

0.105422, 0.590947, 0.465967, Skipping the next 4 numbers... 0.285069, 0.524366, 0.652263 (only shifted by 1 instead of 2 compared to X = 2)

Odd X's:

X = 1

0.105422, 0.590947, 0.465967, Skipping the next 1 numbers... 0.41778, 0.164121, 0.568986 (Completely different set of numbers from X = 0, 2)

X = 3

0.105422, 0.590947, 0.465967, Skipping the next 3 numbers... 0.164121, 0.568986, 0.694944 (only shifted by 1 instead of 2 compared to X = 1)

X = 5

0.105422, 0.590947, 0.465967, Skipping the next 5 numbers... 0.568986, 0.694944, 0.396241 (only shifted by 1 instead of 2 compared to X = 3)


Since coding this test is irrelevant to the assignment itself, I will include my code below (only modified int main() of prng_example.cpp).


In reply to ByungChan Ha

Re: PRNG class not discarding properly?

by Ramses van Zon -
I haven't had a chance to look at this in detail, but make the assignement assuming it's working as it should.
In reply to ByungChan Ha

Re: PRNG class not discarding properly?

by Ramses van Zon -
Finally had a chance to look at this and yes, there is a bug, but the reason is interesting. The random number generator creates 32-bit numbers internally. The std::uniform_real_distribution creates 64-bit doubles. So for each draw of a double, it need to ask the generator for 2 32-but numbers. So to discard `ndiscard` doubles, the randomDouble function should have discarded `2*ndiscard` random numbers.