I am playing around with a pair of algorithms I found on other SO posts (listed in the references below), and am trying to find out how to improve the distribution. I am effectively extending the range of a random number by doubling the number of bits, and want to ensure that the distribution is as uniform as possible, while removing (or at least reducing) the effects of modulo bias and other artifacts for a shuffling algorithm that would be using the result of my modified random number generator.
So, it is to my understanding that if I initialize my RNG with a constant seed (ie: srand(1)
) I will get the same pattern of deterministic outputs from calling rand()
in a for
loop. Now, if I were to initialize my seed via srand(time(NULL))
, it would be a different pattern, but it still might not help me with the following problem: I am trying to figure out if I were to implement the following algorithm:
- Take two random numbers a,b
- Calculate a*(RAND_MAX+1)+b
Would I be able to:
- Generate every possible coordinate pair (a,b), where
a,b ∈ Z+ on [0, RAND_MAX]
(a
andb
are positive integers between zero andRAND_MAX
inclusive). - Maximize the uniformity of the entire distribution (ie: an optimally flat histogram).
While the output of rand()
is supposed to be uniformly distributed, I don't know if it's guaranteed to give me values for N,N+1 calls to rand each loop and give me every pair listing in point (1) before the random sequence repeats itself again. My new random number generator could theoretically generate random values on [0, RAND_MAX ^ 2]
, but I don't know if there might be "holes" aka values in this range that can never be generated by my algorithm.
I attempted to get further on this question myself, but I couldn't find information on how long a random sequence generated be rand()
in C goes until it repeats itself. Lacking this and other information, I couldn't figure out whether or not it is possible to generate every pair (a,b).
So, using rand()
, is it possible to achieve point (1) and if so, are there any solid suggestions on how to optimize its "randomness" according to point (2)?
Thank you for your time and assistance.
Update
I later revisited this problem and simulated it using an 8-bit PRNG. While it could indeed generate every possible coordinate pair, the distribution was actually quite interesting, and definitely not uniform. In the end, I read several articles/papers on PRNGs and used a Mersenne Twiser algorithm to generate the additional bits needed (i.e. MT19937-64).
References
- Extend rand() max range, Accessed 2014-05-07,
<https://stackoverflow.com/questions/9775313/extend-rand-max-range>
- Shuffle array in C, Accessed 2014-05-07,
<https://stackoverflow.com/questions/6127503/shuffle-array-in-c>