Skip to content

Instantly share code, notes, and snippets.

@Vayel Vayel/coin_model.py
Last active Jul 27, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.