Wanted: Gaussian Random Number Generator
Dan Needham
dann at hpsad.HP.COM
Fri Sep 22 08:47:29 AEST 1989
>>>> cj at hpsad.HP.COM (Chris Johnson) writes ......
>>>> I'm looking for a random number routine which will generate
>>>> numbers with a Gaussian distribution. Rand(3C) and other such
>>>> programs produce (semi) even distributions. I suppose some sort
>>>> of conversion routine would also work. If anyone out there has
>>>> such a beast, please let me know.
-cj
cj at hpsad.hp.com
----------
I had to do this once in school and still have the paper I wrote it down on.
This part of the program was handed out by the professor.
function [a,b] = grv(sigma)
% Generates a pair of Gaussian random variables with mean zero, variance sigma
rand('uniform'); % Generates #'s between [0,1]
pi = 3.1415927;
p2 = 2*pi*rand(1);
p1 = (-2) * log(rand(1));
a = sigma * sqrt(p1) * cos(p2);
b = sigma * sqrt(p1) * sin(p2);
------------------------------------------------------------
Dan Needham
More information about the Comp.lang.c
mailing list