2

for my homework assignment I'm to generate a set of numbers to a fixed point that allows for the mean to be 0 and the standard deviation to be 1. I've been mainly dealing with an array of doubles to attempt this but I'm not sure how to accomplish this.

For generating the values, I've tried using Box Muller method but I still end up with a mean and standard deviation thats still off from the mark of mean = 1 and sd = 1 for standard normal distribution

UPDATE: Okay, I just realized my mistake. For box muller, I mistyped the 2 for a 3 instead. Fixed the problem now.

//Create Standard Normal Distribution
void std_find(double a[],double s[], int pop, int samp){//
    //generate random numbers for std normal
    double *p;
    p = a;
    //generate numbers in standard normal
    double numA = 0;
    double numB = 0;
    double PI = 3.14159;
    //Box_Muller
    for(int i = 0; i < pop; i= i+2){
        numA = (rand() * 1)/(double)32767;
        numB = (rand() * 1)/(double)32767;
        *p = sqrt((double)-3 * (log(numB))) * cos((double)3* PI*numA);
        p++;
        *p = sqrt((double)-3 * (log(numB))) * sin((double)3 *PI*numA);
        p++;
    }
    double popMean = mean(a,(double)pop);
    //double popMean = (double)0;
    double popSD = sqrt(sampleV(a,pop));
    //double popSD = (double)1;
    cout << "\nPopulation Mean of the std: " << popMean;
    cout << "\nPopulation Standard deviance of std: " << popSD;
    //population created, now to create sample
    sample(a,s,samp,popMean,popSD);
    }
Bach
  • 5,661
  • 6
  • 25
  • 57
TLM
  • 225
  • 2
  • 7
  • 13
  • 1
    Is it important that a given result set fulfill those criteria (in which case the last number isn't that random anymore), or should the generator do that for an unlimited number of values? – Simon Richter Feb 11 '11 at 10:29

2 Answers2

3

A good general way to go about this is to generate numbers that follow a standard normal distribution (defined as a normal distribution with µ=0 and σ=1). Take a look at the Wikipedia article for the normal distribution, specifically the section that says "Generating values from normal distribution".

Chinmay Kanchi
  • 54,755
  • 21
  • 79
  • 110
  • 3
    Strictly speaking you don't *need* to, there are plenty of other distributions with mean 0 and variance 1. For example, the uniform distribution on the range +/-sqrt(3). But I think you're right that this is what the questioner means :-) – Steve Jessop Feb 11 '11 at 11:21
  • Fair enough, edited to clarify that it is only one reasonable approach to the problem. – Chinmay Kanchi Feb 11 '11 at 11:25
  • I agree with @SteveJessop. Seeing as most PRV generators are uniform, a simple linear transformation of a uniformly distributed "random" number would be the simplest solution. – CB Bailey Feb 12 '11 at 13:48
2

The array of doubles is the easy part, right?

What you need to find is a way to draw a random number from a normalized Gaussian distribution.

Maybe you should look at this:

Generate random numbers following a normal distribution in C/C++

Community
  • 1
  • 1
duffymo
  • 293,097
  • 41
  • 348
  • 541