# monte_carlo_with_dice.py

# We know from math that if you roll a 6-sided die 3 times,
# the odds of the sum being 10 is 27/216, or 0.125.
# Here, we will assume we don't know that, and we will use
# Monte Carlo methods to simulate dice rolls and demonstrate this result.

import random

def rollDie(sides=6):
    return random.randint(1, sides)

def rollDice(times, sides=6):
    result = [ ]
    for i in range(times):
        result.append(rollDie(sides))
    return result

def trialSucceeds():
    dice = rollDice(3)
    return (sum(dice) == 10)

def oddsOfGetting10WithThreeDice(trials=100):
    successes = 0
    for trial in range(trials):
        if trialSucceeds() == True:
            successes += 1
    return successes / trials

trials = 100
answer = oddsOfGetting10WithThreeDice(trials)
realAnswer = 27/216 # from math

print('Estimating the odds of getting at a sum of 10 on 3 dice rolls')
print('Using', trials, 'trials (the more, the better)')
print('Computed answer (from Monte Carlo):', answer)
print('Actual answer (from math):', realAnswer)
print('Accuracy:',1 - abs(answer - realAnswer)/realAnswer)
if (trials < 10000):
    print('*** Increase the # of trials to get more accuracy! ***')
