A linear congruential random number generator.
Uses D.H. Lehmer's Linear Congruential Method (1949) for generating random numbers. Supports both Multiplicative Congruential Method (increment==0) and Mixed Congruential Method (increment!=0) It is perhaps the simplest and fastest method to generate pseudo-random numbers on a computer. Per default uses the values for Minimal Standard LCG. http://en.wikipedia.org/wiki/Linear_congruential_generatorhttp://www.math.rutgers.edu/~greenfie/currentcourses/sem090/pdfstuff/jp.pdf
Pros:
Easy to implement.
Fast.
Cons:
NOT safe for cryptography because of the easily calculatable sequential correlation between successive calls. A case study: http://www.cigital.com/papers/download/developer_gambling.php
Tends to have less random low-order bits (compared to the high-order bits) Thus, NEVER do something like this:
u32 numBetween1And10 = 1 + LCGRand.Int() % 10;
Instead, take into account EVERY bit of the generated number, like this:
u32 numBetween1And10 = 1 + (int)(10.0 * (double)LCGRand.Int() /(LCGRand.Max()+1.0)); or simply
u32 numBetween1And10 = LCGRand.Float(1.f, 10.f);
LCG | A linear congruential random number generator. |
multiplier | |
increment | |
modulus | |
lastNumber | |
ctor (+1 overload) | Initializes the generator from the current system clock. |
Seed(...) | Reinitializes the generator to the new settings. |
Int() | Returns a random integer picked uniformly in the range [0, MaxInt()]. |
MaxInt()[const] | Returns the biggest number the generator can yield. (Which is always modulus-1) |
IntFast() | Returns a random integer picked uniformly in the range [0, 2^32-1]. |
Int(a,b) | Returns a random integer picked uniformly in the range [a, b]. |
Float() | Returns a random float picked uniformly in the range [0, 1[. |
Float01Incl() | Returns a random float picked uniformly in the range [0, 1]. |
FloatNeg1_1() | Returns a random float picked uniformly in the range ]-1, 1[. |
Float(a,b) | Returns a random float picked uniformly in the range [a, b[. |
FloatIncl(a,b) | Returns a random float picked uniformly in the range [a, b]. |