[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
Solved

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

Posted on 2004-11-30
Medium Priority
767 Views
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
0
Question by:sairaseven
• 3
• 2
• 2
• +1

LVL 30

Expert Comment

ID: 12713593
use time to seed the random generator
0

LVL 30

Expert Comment

ID: 12713603
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

LVL 30

Accepted Solution

Axter earned 136 total points
ID: 12713608
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

LVL 4

Expert Comment

ID: 12713647
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 Comment

ID: 12713715
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

LVL 4

Assisted Solution

skypalae earned 132 total points
ID: 12713828
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 Comment

ID: 12723565
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

LVL 6

Assisted Solution

guitaristx earned 132 total points
ID: 12822809
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month17 days, 20 hours left to enroll