-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom3.py
58 lines (40 loc) · 1.23 KB
/
random3.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 5 10:39:02 2020
@author: Paco
"""
# simpleMC.py -- simple Monte Carlo program to make histogram of uniformly
# distributed random values and plot
# G. Cowan, RHUL Physics, October 2019
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
# generate data and store in numpy array, put into histogram
numVal = 10000
nBins = 100
rMin = 0.
rMax = 1.
xMax = 1.
rData1 = np.random.uniform(rMin, rMax, numVal)
rData2 = np.random.uniform(rMin, rMax, numVal)
u1 = rData1
u2 = 2 * rData2
xData = []
for i in range(len(u2)):
if u2[i] <= 2 * u1[i]:
xData.append(u1[i])
xHist, bin_edges = np.histogram(xData, bins=nBins, range=(0, xMax))
# make plot and save in file
binLo, binHi = bin_edges[:-1], bin_edges[1:]
bin_size = binHi[1] - binLo[1]
xPlot = np.array([binLo, binHi]).T.flatten()
yPlot = np.array([xHist, xHist]).T.flatten()
fig, ax = plt.subplots(1,1)
plt.gcf().subplots_adjust(bottom=0.15)
plt.gcf().subplots_adjust(left=0.15)
ax.set_xlim((0, xMax))
ax.set_ylim((0., 2.2))
plt.xlabel(r'$x$', labelpad=0)
plt.plot(xPlot, yPlot/(len(xData) * bin_size))
plt.savefig('xHistAR.png', format='png')
plt.show()