how to change uniformly distributed random variable to exponential or normal distribution

Posted on 2004-11-30
Last Modified: 2012-05-05
    function rand() in C++ generates a random variable which is uniformly distributed but i want to generate a random variable that is exponentialy distributed or normally distributed. can any one tell me the code for doing this.

Question by:sairaseven
    LVL 30

    Expert Comment

    use time to seed the random generator
    LVL 30

    Expert Comment


    #include <time.h>

    int main( void )
       int i;

       /* Seed the random-number generator with current time so that
        * the numbers will be different every time we run.
       srand( (unsigned)time( NULL ) );

       /* Display 10 numbers. */
       for( i = 0;   i < 10;i++ )
          printf( "  %6d\n", rand() );
      return 0;
    LVL 30

    Accepted Solution

    Make sure you only call srand one time throughout the life of your program.
    This should be done in the main() when the program first starts up.
    LVL 4

    Expert Comment


    the original question was not about same rand() values all the time (as usually is) but about the distribution.

    The answer is NO. You may find some algorithms on internet, but you have to implement other distributions by yourself. If I want to use some approximation to normal distribution I usually make mean of several uniformly distributed values:

    randVal = (rand () + rand () + rand () + rand ()) / 4 ;

    This is enough for me (though it is 4x slower than just rand()). If you need more precise solution I'll try to find something else.


    Author Comment

    i want to generate a normally distributed random variable with given mean and variance. moreover, for exponential distribution above solution will not work. if no such function exists then please tell me the algorithm to do that.

    LVL 4

    Assisted Solution

    well ... yes, the above trick doesn't use mean and variance (same as original rand() doesn't). you have to recalculate the result to specific mean and variance by yourself. for example:

    randVal -= RAND_MAX / 2 ; // this makes mean 0
    randVal *= X ; // this sets the variance to X


    anyway. have you tried simple rewriting of mathematical formulas ( ?

    normal distribution:

    x = rand () ;
    m = mean ;
    s = variance ;
    pi = 3.1415 ;
    y = 1 / (s * sqrt (2*pi)) * exp (-0.5 * pow ((x-m)/s, 2)) ;


    x = rand () ;
    l = lambda ;
    y = (x>=0 && l>0)? l * exp (-l * x) : 0 ;


    Author Comment

    well the solution given above don't generate normally distributed random number it just apply a transformation to a uniformly distributed random variable. same is the case with exponential. it did not work
    LVL 6

    Assisted Solution

    rand() gives you a values between a given range (0 - 1), all you've got to do is take that value and apply a function to it.  For instance, if you want a gaussian distribution, simply do this:

    double value = my_gaussian_function(rand());

    The fun part is implementing those functions.  If it were me implementing it, I would use a lookup table of about 256 elements, and do a second-order polynomial extrapolation taking the nearest four values.  Good luck!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now