### Thread: creating a N(0,1) number ??

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2003
Posts
60
Rep Power
11

#### creating a N(0,1) number ??

ok, lets says that I have a bunch of uniform random number from some random number generator. How do I turn these into normally distributed numbers, with mean 0 and variance 1?

(btw i´m still trying to figure out how to make the uniform numbers)

/noze2000
2. It has been a long time since I took statistics, but I don't think you turn the numbers into into a normal distribution, I think they either are normally distributed or not.

If you want to graph the number, first you will have to order them (actually, depending on the code, you don't have to order them first), then you will need to do some sort of frequency analsysis, then plot the results. This example may be of use to you:

Code:
#include <stdlib.h>
#include <stdio.h>

#define NUMDIST 10
#define MAXNUMS 1000000
#define MAX_X 20

void printDist(int *bins, FILE *fo){
int i, j, max=0, numPrint;

//normalize results
for (i=0; i<NUMDIST; i++){
if (max < bins[i]) max = bins[i];
}

//output results
for (i=0; i<NUMDIST; i++){
fprintf(fo, "%4d (%9d) ", i+1, bins[i]);
numPrint = (bins[i] / (float)max) * MAX_X;
for (j=0; j<numPrint; j++)
fputc('X', fo);
fprintf(fo, "\n");
}

return;
}

int main(){
int bins[NUMDIST];
int i, val;

srand(10);//change to get a fresh set of numbers
printf("Iterations: %d\n\n", MAXNUMS);
//zero the input array
for (i=0; i<NUMDIST; i++){
bins[i] = 0;
}
//increment for each found number
//this should generate a 'uniform' distribution
for (i=0; i<MAXNUMS; i++){
val = rand() % NUMDIST;
bins[val]++;
}

printf("\n\nUniform distribution\n");
printDist(bins, stdout);
printf("\n\nNormal distribution\n");
//zero the input array
for (i=0; i<NUMDIST; i++){
bins[i] = 0;
}
//increment for each found number
//this should generate a 'uniform' distribution
for (i=0; i<MAXNUMS; i++){
val = (rand() % NUMDIST) / 3;
val += (rand() % NUMDIST) / 3;
val += (rand() % NUMDIST) / 3;
bins[val]++;
}

printDist(bins, stdout);

return 0;
}
Output:

Code:
Iterations: 1000000

Uniform distribution
1 (    99749) XXXXXXXXXXXXXXXXXXX
2 (   100179) XXXXXXXXXXXXXXXXXXX
3 (   100435) XXXXXXXXXXXXXXXXXXXX
4 (   100259) XXXXXXXXXXXXXXXXXXX
5 (   100250) XXXXXXXXXXXXXXXXXXX
6 (    99772) XXXXXXXXXXXXXXXXXXX
7 (    99795) XXXXXXXXXXXXXXXXXXX
8 (    99926) XXXXXXXXXXXXXXXXXXX
9 (    99927) XXXXXXXXXXXXXXXXXXX
10 (    99708) XXXXXXXXXXXXXXXXXXX

Normal distribution
1 (    26789) XX
2 (    80898) XXXXXXX
3 (   162129) XXXXXXXXXXXXXX
4 (   215423) XXXXXXXXXXXXXXXXXXX
5 (   216257) XXXXXXXXXXXXXXXXXXXX
6 (   161893) XXXXXXXXXXXXXX
7 (    90469) XXXXXXXX
8 (    36060) XXX
9 (     9086)
10 (      996)
The normal distribution is a bit skewed. This may be due to a bug in my code or a problem with rand(). The rand() function supplied in the C library is not a very good one.
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2003
Posts
60
Rep Power
11
What i´m thinking of is some trick like in VB you can use the inverse normal distribution on a random uniform number between [0;1] and get a normally distributed number back which is N(0,1)

I need a bunch of N(0,1) for simulation of option prices (monte carlo)
Last edited by noze2000; September 30th, 2003 at 07:23 AM.
4. I am sure there are statistical libraries that will do that, but this forum is more for generic programming topics.
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2003
Posts
60
Rep Power
11
ok:) i´m quite new at this..thought it wouln´t be much more difficult than rpogramming it in VB when it´s such a small thing.

Though i have no idea where to find those statistical libraries ...?
6. C is a low-level programming language initially written for programming operating systems. It has often been called a "high-level assembly language". C++ has more power and more 'standard' addons, but it also is targeted toward performance oriented coding (though you can write some really crappy code in both without much effort). Higher level languages, such as VB, have lots of built-in functions to make them easier to use. Though I can't give you any hints (other than googling on 'c++' and 'statistic packages') on where to find decent libraries, I can assure you that they are out there and often for free. Just do some research.
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2003
Posts
60
Rep Power
11
I found a few really good ones:) thanks for the hint:)