Skip to content

Instantly share code, notes, and snippets.

@mandyRae
Created February 9, 2016 17:11
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 mandyRae/bc0228c5d2978b02cd45 to your computer and use it in GitHub Desktop.
Save mandyRae/bc0228c5d2978b02cd45 to your computer and use it in GitHub Desktop.
Dice rolling simulation to compare the number experiemental trials vs theoretical probability
'''
Experimental Probability Dice Simulator
This program serves to demonstrate how experimental probability gives
way to theoretical probability as the number of trials increases, using rolls
of a perfect, standard, cube-shaped die. In effect, it demonstrates that the
probability of rolling any number on a perfect die is:
lim x = 1/6 = 0.1666...
x -> infty
where x is the number of experimental rolls.
This program reveals that the more times you roll the die, the closer your
experimental probabilities will be to 1/6.
Notes about the variables and runtime:
The statistics of the currently running simulation are displayed at intervals
as the die rolls. The intervals variable adjusts the number of rolls between
display of the stats. To quickly see how the experimental probabilities
approach 1/6, keep this value high, >10000. Depending on your hardware there will be
a slight pause as the stats render. Displaying the stats more frequently will
cause the the rolling to slow down, but you'll b able to see the results
for rolling a small number of times.
Observations:
Experimental probability reaches theoretical accuracy to 2 decimal places (0.16)
at about 850,000 rolls.
Experimental probability reaches theoretical accuracy to 3 decimal places (0.166)
at about 850,000 rolls.
'''
import time, random
#This var keeps track of the number of rolls for each side of the die
counts = [0,0,0,0,0,0]
#this var keeps track of the total number of rolls
roll = 0
#this will be the currently rolled number
current_roll_val = None
#this starts a timer to be used to computer runtime
start_time = time.time()
#this var adjusts how often the stats are displayed, it's the number of rolls
#between rach display of the stats
interval = 1000000
#this var controls to how many place values the probabilites are rounded to
rounding_digit = 8
def displayStats():
'''
This function displays the current simulation's stats,
runtime, experiemntal probabilities, and total rolls.
'''
print("--Stats--")
print('1 |', counts[0], round(counts[0]/roll, rounding_digit))
print('2 |', counts[1], round(counts[1]/roll, rounding_digit))
print('3 |', counts[2], round(counts[2]/roll, rounding_digit))
print('4 |', counts[3], round(counts[3]/roll, rounding_digit))
print('5 |', counts[4], round(counts[4]/roll, rounding_digit))
print('6 |', counts[5], round(counts[5]/roll, rounding_digit))
print('Time Elapsed: ', round((time.time()-start_time)/60, 2), 'minutes or', round((time.time()-start_time)/(3600), 3), 'hours')
print('Roll count:', roll)
for i in range(0,5):
if counts[i]/roll == (1/6):
print('Probability of rolling', i+1, 'is exactly 1/6')
print()
try:
print('Simulation starting...')
while True:
#choose random of roll, and store it to the current_roll_val
current_roll_val = random.randint(0,5)
#keep track of the rolls of each number by incrementing respective count
counts[current_roll_val] += 1
#increment total number of rolls
roll += 1
if (roll % interval) == 0:
displayStats()
except KeyboardInterrupt:
print('--FINAL RESULTS--')
displayResults()
print()
print("Simulation ending...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment