Skip to content

Instantly share code, notes, and snippets.

@Vayel
Last active July 27, 2018 15:22
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 Vayel/13393f5f21f4f4a6012ae85dfc09e53a to your computer and use it in GitHub Desktop.
Save Vayel/13393f5f21f4f4a6012ae85dfc09e53a to your computer and use it in GitHub Desktop.
import random
# A stochastic model with a single input `p` and a single output `side`
# `p` is the probability of throwing tail
# `side` is either 'head' or 'tail'
def coin_model(p, seed=None):
# The seed allows us to control randomness and ensure reproducibility
random.seed(seed)
# Mathematically, `side_output` is a discrete random variable
# with a Bernoulli distribution B(p) (1 is 'tail' and 0 is 'head')
side_output = lambda: 'tail' if random.random() < p else 'head'
return side_output
if __name__ == '__main__':
coin = coin_model(0.7, seed=123)
runs = 5
for _ in range(runs):
print(coin())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment