tang ju yew
tang ju yew

Reputation: 19

generate a random number from log normal distribution in C/C++

i am using MS Visual studio 2010.

and now I would like to generate a random number in the range from 3 to 200 by a log normal distribution.

I heard that "central limit theorem" can convert the uniform distribution to normal distribution, but it seem too much work for me, because my range has 198 numbers :

a = random(MaxRange+1); // mean i have to write this for 198 time???!!!!
x = (a+.......)/198 ; //this will obtain a number which is a normal distribution right???

then, may i just write

y = log (x);  // and is this mean that y is log normal distribution????

thanks for answering my question....

Upvotes: 1

Views: 4831

Answers (3)

Rahul Madhavan
Rahul Madhavan

Reputation: 303

Further to Azrael3000's answer,

Let the lognormal variable lgn is generated as lgn = exp(mu + sig * stdn) where stdn is the standard normal variable, then we must note that the mu and sig for the equation above are given as:

if m and v are the mean and variance of the non-logarithmized sample values

mu equation

sigma^2 equation

Ref: wiki - Log-normal_distribution

Upvotes: 0

Luca Martini
Luca Martini

Reputation: 1474

You may want to look at the lognormal_distribution class inside Boost random library. See here for an example of how to generate numbers from a given distribution (you have to instantiate a boost::variate_generator with a given random number generator plus an instance of the distribution).

Upvotes: 2

Azrael3000
Azrael3000

Reputation: 1897

Well random will give you uniformly distributed random numbers as you said correctly. In order to generator variables with normal distribution you can use the Box-Muller transformation which is simple to implement.

Next you need to generate your lognormal variable v. By calculating v = exp(mu + sig * n) where n is your normal distributed random variable.

I don't quite understand what you mean with range 3 to 200 as the lognormal distribution has support ]0,inf[

Upvotes: 3

Related Questions