Random Number Generators - Uniform and Gaussian
See Wilks, Daniel S., Statistical Methods in the Atmospheric Sciences, 2nd Edition, Section 4.7, which
can be found at www.atmosfera.unam.mx/jzavala/AnalisisDatos/Statistical_Methods_Wilks.pdf
3rd edition on line version available through library.
AND
Numerical Recipies (http://numerical.recipes/) - codes available for C++ and Fortran.
-----------------------------------------------------Uniform random number generators: Various methods, READ
http://apps.nrbook.com/fortran/index.html Chapter 7 or equivalent , especially Section 7.2
dx/dy = f(y)
ALSO see "https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform" for explanations.
Output from rand (251 points!)
S eries
1.00
.80
.60
.40
.20
.00
0
50
100
150
200
250
35.
30.
25.
20.
15.
10.
5.
.0
.2
.4
.6
.8
1.0
Histogram (12 bins but cannot specify range? Poor options in ISTM)
R esidualA C F:A bs values
1.00
R esidualA C F:S quares
1.00
.80
.80
.60
.60
.40
.40
.20
.20
.00
.00
-.20
-.20
-.40
-.40
-.60
-.60
-.80
-.80
-1.00
-1.00
0
5
10
15
20
25
30
35
40
0
5
10
15
20
25
30
Gaussian distribution Box-Muller code 251samples
S eries
3.
2.
1.
0.
-1.
-2.
-3.
0
50
100
150
200
250
Histogram (20 bins)
45.
40.
35.
30.
25.
20.
15.
10.
5.
-3
-2
-1
0
1
2
3
Autocorrelatiion
R e s id u a l A C F
1.00
R
1.00
.80
.80
.60
.60
.40
.40
.20
.20
.00
.00
-.20
-.20
-.40
-.40
-.60
-.60
-.80
-.80
-1.00
-1.00
0
5
Sample size allowed in ITSM (student version) is 251 max
10
15
20
25
30
35
40
0
5
10
15
With 1536 samples, using Grapher
4
2
0
-2
-4
0
400
800
1200
1600
Mean = -0.0116,, St Dev = 1.0136 : Still need more samples to get really close to 0, 1
200
160
120
80
40
0
-3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 3 3.5
Histogram 28 bins
My matlab code - could certainly be improved!
% Peter Taylor
clc;
n = 1000
j=0;
rand('seed',n)
for i = 1:n
RR1=rand;
RR2=rand;
g(i,1)=i;
g(i,2) = RR1;
g(i,3) = RR2;
R1 = 2.0*RR1 - 1.0;
R2 = 2.0*RR2 - 1.0;
RSQ = R1*R1+R2*R2;
flag = 1;
if ((RSQ >= 1.0) || (RSQ == 0.0))
flag = 0;
else
j=j+1;
fac = sqrt(-2*log(RSQ)/RSQ);
gset = R1*fac;
gasdev = R2*fac;
f(j,1)=i;
f(j,2)=flag;
f(j,3)=gset;
f(j,4)=gasdev;
end
end
Mg = mean(g)
Vg = var(g)
Mf = mean(f)
Vf = var(f)
csvwrite('F:\4020-2016\taylor-RF.csv',f);
csvwrite('F:\4020-2016\taylor-RG.csv',g);
%plot(x,f,'-')
I combined gset and gasdev into a single series for plots.
© Copyright 2026 Paperzz