-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex6.3.py
More file actions
33 lines (29 loc) · 858 Bytes
/
ex6.3.py
File metadata and controls
33 lines (29 loc) · 858 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 16 17:11:58 2020
@author: astro
"""
import random
import math
import matplotlib.pyplot as plt
N = int(1e4)
sig = 2
mmax = 50.
mmin = -50.
X = []
k = 0
while (k<N):
a=random.random()
z = a*(mmax-mmin)+mmin # uniform random number within max and min
m=random.random() # uniform random number between 0 and 1
p = (1/(2*math.pi)**0.5/sig*math.exp(-z**2/2/sig**2))
#calculate gaussian distribution using the 1st random number y
if (m<=p):
X.append(z) #if random m is smaller than the gaussian, add it to list
k+=1 #that means the value is inside our desired distribution
#keeps up until it has the chosen number N of gaussian distributions added to list
plt.hist(X,bins=60,density='True')
plt.xlabel("x")
plt.ylabel("PDF(x)")
plt.show()