Skip to content

Instantly share code, notes, and snippets.

@kheremos
Last active March 17, 2026 21:14
Show Gist options
  • Select an option

  • Save kheremos/df5a7511999a08fb4416e842bb6ecdf9 to your computer and use it in GitHub Desktop.

Select an option

Save kheremos/df5a7511999a08fb4416e842bb6ecdf9 to your computer and use it in GitHub Desktop.
Fixed Example from an ancient Python book from Chris Fehily
# Reference: Python (c) 2002, page xviii
import random
rand = random.random
experiments, flips = 500, 16
heads = [0] * (flips +1)
print(f"Flips: {flips}, len(heads): {len(heads)}, \nheads: {heads}")
for i in range(experiments):
count, toto = 0, 0
# Previously for j in range(len(heads))
for j in range(flips):
if rand() < 0.50: count += 1
heads[count] += 1
for j in range(flips + 1):
print("%2d " %j,end="")
i = 0
while i < heads[j]:
print("*",end="")
i += 10
print()
@kheremos

kheremos commented Mar 16, 2026

Copy link
Copy Markdown
Author

Line 3/11 was uneccessary, I just like the way "rand()" looks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment