Last active
March 17, 2026 21:14
-
-
Save kheremos/df5a7511999a08fb4416e842bb6ecdf9 to your computer and use it in GitHub Desktop.
Fixed Example from an ancient Python book from Chris Fehily
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
| # 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() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 3/11 was uneccessary, I just like the way "rand()" looks.