Direct surface 2d.py

From Werner KRAUTH

Jump to: navigation, search

Context

This page is part of my 2024 Beg Rohu Lectures on "The second Markov chain revolution" at the Summer School "Concepts and Methods of Statistical Physics" (3 - 15 June 2024).

Python program

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()
Personal tools