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

hi,
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.

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

Commented:
use time to seed the random generator
0
Commented:
Example:

#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;
}
0
Commented:
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.
0

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.

Commented:
Axter,

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.

S.
0
Author 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.

saira
0
Commented:
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 (http://astronomy.swin.edu.au/~pbourke/analysis/distributions/) ?

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)) ;

exponential:

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

S.
0
Author 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
0
Commented:
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!
0
###### 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
C++

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.