From Werner KRAUTH
Revision as of 12:48, 6 June 2024;
view current revision←Older revision |
Newer revision→
import random, math
import matplotlib.pyplot as plt
N_trials = 1000000
data = []
twopi = 2.0 * math.pi
for iter in range(N_trials):
x = random.gauss(0.0, 1.0)
y = random.gauss(0.0, 1.0)
r = math.sqrt(x **2 + y ** 2);
x = x / r; y = y / r # uniform sample on the surface of unit sphere
phi = (math.atan2(y, x) + twopi) % twopi
data.append(phi)
plt.title('direct_surface_2d.py (histogram of angles)')
plt.xlabel('angle')
plt.ylabel('histogram')
plt.hist(data, bins=100, density=True)
plt.show()