In most programming languages, you can create separate random number generators:

Random r1 = new Random(0);
Random r2 = new Random(13);

I was looking at the R language, and as far as I can tell, there’s only one global RNG so there’s no way to create multiple generators for a script (I could be wrong – the R documentation is very weak on this topic).

By far the simplest random number generator algorithm is called the Lehmer algorithm. Briefly, X(n+1) = a * X(n) mod m. In words, to get a new random number, take the current number, multiply by some number a, then take that modulo some number m. The tricky parts are finding good values of a and m, and avoiding arithmetic overflow when multiplying.

Both of these issues are explained in the famous article “Random Number Generators: Good Ones are Hard to Find.” While I was sitting in an airport, waiting to board a flight, I coded up the Lehmer algorithm (using a = 48271 and m = 2147483647) in C#. I used C# because it has fewer quirks than R and random number generators can be very, very, very tricky.

An interesting alternative to Lehmer is the Lagged Fibonacci algorithm. Lagged Fibonacci typically uses Lehmer for an initialization process. I’ll take a look at Lagged Fibonacci when I get a few free minutes.

### Like this:

Like Loading...

*Related*