Created
January 27, 2021 18:29
-
-
Save MaxHalford/03449a41d4e98433b1d7568040961171 to your computer and use it in GitHub Desktop.
Pure Python random laws
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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