Skip to content

Instantly share code, notes, and snippets.

@inoryy
Created March 16, 2016 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inoryy/ef43549d91bdfb757393 to your computer and use it in GitHub Desktop.
Save inoryy/ef43549d91bdfb757393 to your computer and use it in GitHub Desktop.
Normal Distribution via Box–Muller transform
from random import random
from math import log, cos, sin, sqrt, pi
# Implementation of Box-Muller transform to
# generate two random numbers from N(mu, sigma) distribution
def pnorm(mu = 0, sigma = 1):
r = sqrt(-2*log(random()))
a = 2*pi*random()
return sigma*r*cos(a) + mu, sigma*r*sin(a) + mu
@inoryy
Copy link
Author

inoryy commented Mar 16, 2016

import matplotlib.pyplot as plt

x = []
for _ in range(50000): x += pnorm(0,1)
plt.hist(x)
plt.show()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment