Skip to content

Instantly share code, notes, and snippets.

@MobCat
Created July 6, 2022 08:55
Show Gist options
  • Select an option

  • Save MobCat/1f970018c5fc6829174a09c5e96e9dbe to your computer and use it in GitHub Desktop.

Select an option

Save MobCat/1f970018c5fc6829174a09c5e96e9dbe to your computer and use it in GitHub Desktop.
Flip X number of coins and then count them. Or flip 0 number of coins and count forever.
#!/env/Python3.10.4
#/MobCat (2022)
import random
from datetime import datetime
heads = 0
tails = 0
total = 0
number = input("Number of times to flip coin: ")
start = datetime.now()
try:
if int(number) == 0:
while True:
flip = random.randint(0, 1)
if (flip == 0):
heads += 1
else:
tails += 1
total += 1
quotient = heads/total
percent = quotient * 100
quotient2 = tails/total
percent2 = quotient2 * 100
print(f"total={total} heads={heads}({str(round(percent, 2))}%) tails={tails}({str(round(percent2, 2))}%) runtime={datetime.now()-start} ", end='\r')
else:
for i in range(int(number)):
flip = random.randint(0, 1)
if (flip == 0):
heads += 1
else:
tails += 1
total += 1
quotient = heads/total
percent = quotient * 100
quotient2 = tails/total
percent2 = quotient2 * 100
print(f"total={total} heads={heads}({str(round(percent, 2))}%) tails={tails}({str(round(percent2, 2))}%) runtime={datetime.now()-start} ", end='\r')
print("\ndone.")
except KeyboardInterrupt:
print("\nScript terminated by user.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment