-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNormal_random_numbers.py
More file actions
40 lines (33 loc) · 954 Bytes
/
Copy pathNormal_random_numbers.py
File metadata and controls
40 lines (33 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Normal random numbers
#
# Author: Oscar A. Nieves
# Last updated: July 01, 2021
import matplotlib.pyplot as plt
import numpy as np
plt.close('all')
np.random.seed(0) # Set seed
# Inputs
mu = 0.5
sigma = 0.75
samples = 100000
# Random samples (Uniformly distributed)
A = np.random.rand(samples,1)
B = np.random.rand(samples,1)
# Normal random numbers N(mu, sigma^2)
X = mu + sigma * np.sqrt(-2*np.log(A))*np.cos(2*np.pi*B)
Y = mu + sigma * np.sqrt(-2*np.log(A))*np.sin(2*np.pi*B)
# Compute statistics
EX = round(np.mean(X),2)
VarX = round(np.var(X),2)
EY = round(np.mean(Y),2)
VarY = round(np.var(Y),2)
# Plot histograms
bins = 50
plt.subplot(1,2,1)
plt.hist(X,bins)
plt.xlabel('X ~ N(mu = ' + str(EX) + ', sigma^2 = ' + str(VarX) + ')', fontsize=16)
plt.ylabel('frequency', fontsize=16)
plt.subplot(1,2,2)
plt.hist(Y,bins)
plt.xlabel('Y ~ N(mu = ' + str(EY) + ', sigma^2 = ' + str(VarY) + ')', fontsize=16 )
plt.ylabel('frequency', fontsize=16)