Visual C++ Log Normal Distribution

• 03-17-2012
juyew
Visual C++ Log Normal Distribution
hey, now i using the Visual Studio 2010 C++
i would like to random generate a number from log normal distribution, but so far i only know to random generate a number from normal distribution by:

rand() / rand_max

rite???? then, i dont know how to do for log normal distribution...
pls help me, tq
• 03-17-2012
oogabooga
rand()/RAND_MAX does not give a "normal distribution". It is simply linear. Here are some ideas for a normal distribution.
• 03-17-2012
bernt
Visual Studio 2010 has support for the <random> header, which is now part of the C++11 standard. <random> has support for lognormal distributions, among many other things; here's a quick example of its usage:

Code:

```#include <random> #include <ctime> ... std::mt19937 randgen; randgen.seed(time(NULL)); //the parameters are (mean, standard deviation) - default is (0.0, 1.0) std::lognormal_distribution<float> lognorm(0.0, 0.25); float result = lognorm(randgen);```
• 03-17-2012
iMalc
rand() / (double)RAND_MAX gives what is known as a "uniform distribution" because rand() is itself a uniform distribution.
• 03-18-2012
juyew
Quote:

Originally Posted by bernt
Visual Studio 2010 has support for the <random> header, which is now part of the C++11 standard. <random> has support for lognormal distributions, among many other things; here's a quick example of its usage:

Code:

```#include <random> #include <ctime> ... std::mt19937 randgen; randgen.seed(time(NULL)); //the parameters are (mean, standard deviation) - default is (0.0, 1.0) std::lognormal_distribution<float> lognorm(0.0, 0.25); float result = lognorm(randgen);```

tq...it work so far....
but if i want the number range from 3 to 200, how to do?
bcz now i using lognorm(31.1, 0.48), but it result to me is too huge,
tq
• 03-18-2012
juyew
Quote:

Originally Posted by iMalc
rand() / (double)RAND_MAX gives what is known as a "uniform distribution" because rand() is itself a uniform distribution.

so it is the same uniform distribution and what is the diff if i write so:

x = rand()%4+1;

and

x = (rand()%4+1)/RAND_MAX;

but for program with RAND_MAX, i oways get the value of 0, is it i written it wrong? or should i define the RAND_MAX 1st???

tq very much for answer it