Last active
May 23, 2021 08:41
-
-
Save thomasaarholt/b47251318558eccbee527e13469eed34 to your computer and use it in GitHub Desktop.
If a number of hens in a circle peck either left or right, what is the expected number of unpecked hens at the end?
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 numpy as np | |
import random | |
number_of_hens = 100 | |
repeats = 1000 # repeat many times so that we get some decent statistics | |
sums = [] | |
for _ in range(repeats): | |
hens = np.zeros(number_of_hens, dtype=bool) # reset an array of False | |
# for each hen, peck either left or right | |
for i, hen in enumerate(hens): | |
if random.random() < 0.5: # random() returns a number between 0 and 1 | |
index = (i-1) % number_of_hens # pecks left | |
else: | |
index = (i+1) % number_of_hens # pecks right | |
hens[index] = True | |
sums.append(number_of_hens - hens.sum()) # add "number of unpecked hens" to a list. (array_of_bools).sum() sums to ints | |
print(f"The mean is {np.mean(sums)}, with a standard error of {np.std(sums) / np.sqrt(number_of_hens):.2f}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment