Generating Gaussian Random Numbers

In Matlab

You can generate a k by n array of Gaussian random numbers with mean zero and variance 1 by
randn(k,n)

In Java

It's a lot like generating uniform random numbers. The following snippets of code return a Gaussian random number with mean 0 and standard deviation 1. Each time you call r.nextGaussian(), you get another.
import java.util.*;
Random r = new Random();
g = r.nextGaussian();

In C

I recommend the routine gasdev from Numerical Recipies in C to generate Gaussian random variables. My program, randGen.c makes use of this routine. This program takes three inputs: a 0/1 flag, a number n, and a random seed. n is the number of outputs it will generate. seed is a seed for the generator. The flag determines whether to output Gaussian random numbers or numbers uniformly chosen between 0 and 1. If the flag is 1 it outputs Gaussians. Here is a demo:
[dan@localhost ECC]$ date +%s
1032283634
[dan@localhost ECC]$ ./randGen 0 10 1032283634
0.293738
0.003762
0.657379
0.566909
0.247530
0.053829
0.768945
0.506524
0.983858
0.380157
[dan@localhost ECC]$ date +%s
1032283655
[dan@localhost ECC]$ ./randGen 1 10 1032283655
-1.095767
1.311092
1.863238
0.743954
1.141838
1.354877
-0.840446
-0.796139
1.132950
0.209844
To learn something about this data, let's import it into matlab and plot a little. We'll do it by saving it in to a file, like,
[dan@localhost ECC]$ date +%s
1032311720
[dan@localhost ECC]$ ./randGen 0 100000 1032311720 > uniform.txt
[dan@localhost ECC]$ ./randGen 1 100000 1032311720 > gaussian.txt
To then import this in to matlab, you could then type
>> u = load('uniform.txt');
>> g = load('gaussian.txt');
Now, you can proceed with the explorations suggested in Probability Distributions.
Dan Spielman
Last modified: Wed Apr 14 12:56:51 EDT 2004