Subscribe to DSC Newsletter

Simulating Distributions with One-Line Formulas, even in Excel

If you don't like using black-box R functions, or you don't have access to these functions, here are simple options to simulate deviates from various distributions. They can even be implemented in Excel! You first need to simulate uniform deviates on [0, 1]. If you don't trust the function available in your programming language, here is a good alternative:

rnd = 1000
for (n=0; n<20000; n++) {
     rnd=(10232193 * rnd + 3701101) % 54198451371
     Rand= rnd / 54198451371


This code produces 20,000 deviates of a uniform distribution on [0, 1].  The deviates are stored in the variable named Rand. The symbol % stands for the modulo operator.

Now, assuming Rand, Rand1 and Rand2 are uniform deviates on [0, 1], here is how to sample deviates from various other distributions:

Normal(0, 1) and log-normal deviates:

  • Normal: x = sqrt(-2* log(Rand1)) * cos(2* Pi *Rand2)   
  • Log-normal: y = exp(x)

Exponential deviates of parameter Lambda:

  • x = - log(1 - Rand) / Lambda

Geometric deviates of parameter P:

  • if (Rand < P) { x = 0 } else { x = int(log(1 - Rand) / log(1 - P)) }

Power law deviates with exponent B, on [0, A]:

  • x = A * Rand^(1 / B)

Do you know any simple formula to generate other types of deviates?

Views: 1055

Reply to This

Replies to This Discussion

Can you recommend any references you can recommend that list parameters for Linear Congruent Generators and document their statistical performance? Most sources (like Wikipedia) list parameters with little discussion of statistical properties.

Hi Dennis, the choice of the parameters in my linear congruent generator is a little arbitrary. I played with various values until I found ones that give a long enough period. So my solution is not spectacular by any means, especially since I was just trying to get a far better generator than the one available in the Perl programming language (that one can only produce 32,767 different values.)

I'm researching the topic for a project, and will be happy to share my notes.
Basically, I need enough documentation to satisfy clients (including convincing them that we don't need a cryptographic grade random number generator).
I thought this was a good paper and references some pretty robust methods.

Thanks Dennis, and if you find a better one than mine, please share. Should be easy. Likewise, I don't need weapon-grade cryptography.

I've decided to use the Wichmann-Hill 2006 random number generator for my application. Not the earlier 1982 version, which used to be commonly used (even through Python v2.2), before the dominance of the Mersenne-Twister algorithm (the 1982 version is still available in R, not the 2006).

While the 1982 version uses a 3 2-byte integer state variable (for a cycle length of ~7x10^12), the 2006 version uses 4 4-byte integer state variables (for a cycle length of ~1.3x10^36). They also provide an easy method for spinning off new seeds for independent parallel sequences of random numbers (with no overlap for >2.3x10^18 values).

The 64 bit arithmetic is not a problem for R, but I have not optimized it using rcpp, so it runs a bit slow.

Also, I have not found any further documentation of anyone using it or testing it further. (Wichmann says it passes DIEHARD and the TESTU01 random number generator test sets).

Their code (Ada?) is:

ix := 11 600 × ix mod 2 147 483 579;

iy := 47 003 × iy mod 2 147 483 543;

iz := 23 000 × iz mod 2 147 483 423;

it := 33 000 × it mod 2 147 483 123;

W := ix/2 147 483 579.0 + iy/2 147 483 543.0 + iz/2 147 483 423.0 + it/2 147 483 123.0;

return W −⌊W⌋.

For parallelization, they just multiply the ix and iy seeds with different primes, yielding a new seed state for each sequence, and guaranteeing no overlap for a very long time.

ix := 46 340 x ix mod 2 147 483 579;

iy := 22 000 x iy mod 2 147 483 543;

Researchgate has the paper:

B.A. Wichmann, I.D. Hill, Generating good pseudo-random numbers,
Computational Statistics & Data Analysis, Volume 51, Issue 3, 2006,



  • Add Videos
  • View All

© 2020   Data Science Central ®   Powered by

Badges  |  Report an Issue  |  Privacy Policy  |  Terms of Service