I play with two algorithms that I found on other SO posts (listed in the links below), and try to figure out how to improve distribution. I am effectively expanding the random number range by doubling the number of bits and want the distribution to be as uniform as possible, removing (or at least reducing) the effects of modular bias and other artifacts for the shuffle algorithm that will use the result of my modified random number generator.
So, I understand that if I initialize my RNG with a constant seed (i.e.: srand(1) ), I will get the same pattern of deterministic exits from the rand() call in a for loop. Now, if I initialized my seed via srand(time(NULL)) , it would be a different template, but it still may not help me with the following problem: I am trying to figure out if I should implement the following algorithm:
- Take two random numbers a, b
- Calculate a * (RAND_MAX + 1) + b
Can I:
- Create all possible coordinate pairs (a, b), where
a,b ∈ Z+ on [0, RAND_MAX] ( a and b are positive integers from zero to RAND_MAX inclusive). - Increase the uniformity of the entire distribution (i.e., an optimally flat histogram).
While the output of rand() should be evenly distributed, I don't know if it guaranteed to give me values for N, N + 1 calls of rand in each loop and give me a list of each pair at point (1) before random sequence will be repeated again. My new random number generator can theoretically generate random values on [0, RAND_MAX ^ 2] , but I don’t know if there can be “holes” like values in this range that can never be generated by my algorithm.
I tried to continue this question myself, but I could not find information on how long a random rand() sequence was generated in C until it repeated. Without this and other information, I could not understand whether it is possible to generate each pair (a, b).
So, using rand() , is it possible to reach point (1), and if so, are there any solid suggestions on how to optimize your “randomness” according to point (2)?
Thanks for your time and help.
Update
Later I reviewed this problem and modeled it using an 8-bit PRNG. Although he could indeed generate all possible coordinate pairs, the distribution was really quite interesting and, of course, uneven. In the end, I read several articles / articles on PRNG and used the Mersenne Twiser algorithm to generate the extra bits needed (i.e. MT19937-64).
References
- Extend range rand () max, Available 2014-05-07,
< /questions/813325/extend-rand-max-range > - Shuffle array in C, Accessed 2014-05-07,
< /questions/168552/shuffle-array-in-c >
c algorithm random statistics
Devnull
source share