Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pseudorandom Number Generator - Exponential Distribution

I would like to generate some pseudorandom numbers and up until now I've been very content with the .Net library's Random.Next(int min, int max) function. PRNGs of this variety are supposed to be using a Uniform distribution, but I would very much like to generate some numbers using an Exponential Distribution.

I'm programming in C#, although I'll accept pseudocode or C++, Java or the like.

Any suggestions / code snippets / algorithms / thoughts?

like image 751
Charlie Salts Avatar asked Jan 21 '10 02:01

Charlie Salts


People also ask

How do you generate pseudorandom numbers?

An early computer-based PRNG, suggested by John von Neumann in 1946, is known as the middle-square method. The algorithm is as follows: take any number, square it, remove the middle digits of the resulting number as the "random number", then use that number as the seed for the next iteration.

Is pseudorandom number generator random?

Pseudo Random Number Generator (PRNG) Software-generated random numbers only are pseudorandom. They are not truly random because the computer uses an algorithm based on a distribution, and are not secure because they rely on deterministic, predictable algorithms.


2 Answers

Since you have access to a uniform random number generator, generating a random number distributed with other distribution whose CDF you know is easy using the inversion method.

So, generate a uniform random number, u, in [0,1), then calculate x by:

x = log(1-u)/(-λ),

where λ is the rate parameter of the exponential distribution. Now, x is a random number with an exponential distribution. Note that log above is ln, the natural logarithm.

like image 58
Alok Singhal Avatar answered Sep 24 '22 19:09

Alok Singhal


The Fundamental Theorem of Sampling holds that if you can normalize, integrate and invert the desired distribution you are home free.

If you have a desired distribution F(x) normalized on [a,b]. You compute

C(y) = \int_a^y F(x) dx 

invert that to get C^{-1}, throw z uniformly on [0,1) and find

x_i = C^{-1}(z_i) 

which will have the desired distribution.


In your case: F(x) = ke^{-kx} and I will assume that you want [0,infinity]. We get :

C(y) = 1 - e^{-ky} 

which is invertable to give

x = -1/k  ln(1 - z) 

for z thrown uniformly on [0,1).


But, frankly, using a well debugged library is smarter unless you're doing this for your own edification.

like image 45
dmckee --- ex-moderator kitten Avatar answered Sep 21 '22 19:09

dmckee --- ex-moderator kitten