Sampling from normal distribution

Many a times we need to generate random samples or rather random numbers, which essentially follow or approximate a normal distribution. Such random numbers find extensive applications in the fields of statistics, probability, gaming etc.

We present here  a very small function written in C/C++, which will generate pseudorandom numbers following a normal distribution having mean as zero(0) and given variance (bsq). As the code uses very basic statements, same can be easily tailored for use in other programming languages also.

//Sampling in a normal distribution with mean 0 and variance bsq
double normalSampling (double bsq)
double sum=0, tempB=0,b;
int i;
for (i=1; i<=12; i++)
srand(time(NULL)* i + rand());
// generate a random double number between 0 and 1
tempB=2 * b * tempB;// to generate a random no between 0 and 2b
tempB=tempB-b;//to ensure random no generated falls between -b to b
sum+=tempB;    // add to the cumulative sum
return sum;

Leave a Reply

You may also connect using:
login to using facebook