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

    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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

use time to seed the random generator

#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;
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.


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.

sairasevenAuthor Commented:
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.

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 ;

sairasevenAuthor Commented:
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
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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.