Radnom Number Generator

Which Random Number Generator would be best? By best I mean simplest and non-repeating!
All PRNGs repeat eventually since they are all based on seeds that are of finite length. What kind of period do you want? (ie, what is the minimum number of RNs you want to generate before it repeats?) The C RNG has
period 2^32.

What would you constitute as a "simple" PRNG?
Randoms I need would be between 1 and 4, and the seconds randoms from 1 to
13...
Btw, you say that all generators repeat eventually... Isn't that avoidable?
I'm total newb =P
I don't know for what purpose you need a RNG, but it seems, like the standard number generator rand() would be good enough.
http://www.cplusplus.com/reference/clibrary/cstdlib/rand.html

Yes, all PRNGs repeat be design. It has to be that way, because there is only a finite amount of numbers, that can be stored in a value, and PRNGs behave deterministic. But PRNGs vary in the length of the period (normaly 2^32 is enough for almost any purpose) and in the randomness (-> autocorrelation). There are still plenty of tricks to do statistical test and increase the randomness.
http://en.wikipedia.org/wiki/Autocorrelation

Really strong and fast RNGs with high randomness are needed for example in particle physics to do Monte-Carlo-Simulation, where any kind of autocorrelation can disturb your whole simulation. For special purposes like this i can recommend ranlux from cern.
http://wwwasdoc.web.cern.ch/wwwasdoc/shortwrupsdir/v115/top.html
http://cernlib.web.cern.ch/cernlib/
A popular and fast PRNG is Mersenne Twister (http://en.wikipedia.org/wiki/Mersenne_Twister ), with a period of 219937-1.
tnx guys I think I've found what I was looking for =)

-EDIT-
I've found it but i don't understand something so if you could possibly explain me what this line does i will be grateful xD...

iSecret = rand() % 10 + 1;

I know it generates a random number but if I want a number from, let's say, 10-20 what numbers should I put instead of 10 and 1?
TNX again =P
Last edited on
rand() % 10 returns a random number in [0,10)
so adding 1 will be the result be in [1,11) so you will get a random number from 0 to 10. You can do something similar if you wand the number to be in [10,20]
The % operator returns the value 'thats left', eg 8%3=2 (because 3*n+2=8, the value of n is 2 of course, but that doesn't matter for the result).
And note you need to use srand() when your program starts. If you don't do this already: srand() seeds the RNG, and when you initialize it with the same values the same numbers will come out. time(NULL) from <time.h> is often used as argument for srand(); this function returns the amount of seconds since the first day of 1970 (and so it's different each second): srand(time(NULL))
Topic archived. No new replies allowed.