Thread: Visual C++ Log Normal Distribution

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    3

    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

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    rand()/RAND_MAX does not give a "normal distribution". It is simply linear. Here are some ideas for a normal distribution.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    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);
    Last edited by bernt; 03-17-2012 at 04:36 PM. Reason: derped the code
    Consider this post signed

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    rand() / (double)RAND_MAX gives what is known as a "uniform distribution" because rand() is itself a uniform distribution.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    Quote Originally Posted by bernt View Post
    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

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    Quote Originally Posted by iMalc View Post
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generate numbers according to Normal Distribution
    By anirban in forum C Programming
    Replies: 1
    Last Post: 11-27-2010, 08:53 AM
  2. Normal maps: Get normal x/y/z from color
    By Devils Child in forum Game Programming
    Replies: 2
    Last Post: 08-09-2009, 12:01 PM
  3. Some .NET Distribution Stats
    By nickname_changed in forum C# Programming
    Replies: 2
    Last Post: 05-14-2005, 03:41 AM
  4. standard Normal Distribution
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 05-21-2002, 11:45 AM
  5. Best Distribution
    By gnu-ehacks in forum Linux Programming
    Replies: 4
    Last Post: 11-21-2001, 03:59 AM