Buggy surface 2d.py

From Werner KRAUTH

Revision as of 12:46, 6 June 2024; view current revision
←Older revision | Newer revision→
Jump to: navigation, search
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.uniform(-1.0, 1.0)
    y = random.uniform(-1.0, 1.0)
    r = math.sqrt(x **2 + y ** 2);
    x = x / r; y = y / r # non-uniform sample on the surface of unit sphere
    phi = (math.atan2(y, x) + twopi) % twopi
    data.append(phi)
plt.title('buggy_surface_2d.py (histogram of angles)')
plt.xlabel('angle')
plt.ylabel('histogram')
plt.hist(data, bins=100, density=True)
plt.show()
Personal tools