Skip to content

Instantly share code, notes, and snippets.

@MaxHalford
Created January 27, 2021 18:29
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 MaxHalford/03449a41d4e98433b1d7568040961171 to your computer and use it in GitHub Desktop.
Save MaxHalford/03449a41d4e98433b1d7568040961171 to your computer and use it in GitHub Desktop.
Pure Python random laws
import math
import random
def poisson(l, rng=random):
"""From https://www.johndcook.com/blog/2010/06/14/generating-poisson-random-values/"""
L = math.exp(-l)
k = 0
p = 1
while p > L:
k += 1
p *= rng.uniform(0, 1)
return k - 1
poisson(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment