Skip to content

Instantly share code, notes, and snippets.

@thomasaarholt
Last active May 23, 2021 08:41
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 thomasaarholt/b47251318558eccbee527e13469eed34 to your computer and use it in GitHub Desktop.
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?
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