Gaussian random values. Experiment with the following function for generating random variables from the Gaussian distribution, which is based on generating a random point in the unit circle and using a form of the Box-Muller formula (see Exercise 1.2.24)
def gaussian():
r = 0.0
while (r >= 1.0) or (r == 0.0):
x = -1.0 + 2.0 * random.random()
y = -1.0 + 2.0 * random.random()
r = x*x + y*y
return x * math.sqrt(-2.0 * math.log(r) / r)
Take a command-line argument n and generate n random numbers, using an array a [ ] of 20 integers to count the numbers generated that fall between i * .05 and (i+1) * .05 for i from 0 to 19. Then use stddraw to plot the values and to compare your result with the normal bell curve.
using the book site modules stdarray and stdio to implement the functions.
A Gaussian distribution with mean zero and standard deviation
one, often known as
a “standard normal” distribution, has the probability density
function (PDF):
φ(x) = 1
√
2π
e−x2/2. (1)
A plot of φ(x) versus x gives the familiar bell-curve shape, but
does not directly indicate
the probability of occurrence of any particular range of values of
x.
from −∞ to x gives the cumulative distribution function
(CDF):
(x) =
-
x
−∞
φ(x)dx = 1
2
1 + erf x
√
2
Algorithm 2. Monty Python
1: s ← 22U1 − 1 {Choose random sign (+1 or −1) for output
sample}
2: x ← bU2 {Horizontal component of uniform 2D random sample}
3: if x < a then {Check if point is in area A}
4: return sx
5: end if
6: y ← U3/(2b) {Vertical component of uniform 2D random
sample}
7: if y < φ(x) then {Check if point is under Gaussian PDF in
area B}
8: return sx
9: end if
10: (x, y) ← fC(x, y) {Point is in region C
, transform it to region C}
11: if y < φ(x) then {Check if point is under Gaussian PDF in
area C}
12: return sx
13: else
14: return Return x from the tails with |x| > b (see section
3)
15: end if
Get Answers For Free
Most questions answered within 1 hours.