Stanislaw Ulam (mathematician) was recovering from illness and while playing Solitaire card game multiple times he was trying to estimate a probability of winning. Because winning the game was such a rare event, he needed to play thousands of games which wasn't practical. He started wondering if it was possible to simulate a lot of games on the computer and then simply observe the number of successful outcomes. Monte Carlo computation method was born.
I want to try using Monte Carlo method to calculate the probability of having the same heads after rolling two dice.
First I going to define a Class with all possible variable and methods:
import random
class Dice():
def __init__(self):
self.sides = 6
self.top_side = None
def roll(self):
self.top_side = random.randrange(0,self.sides)
def __repr__(self):
return 'Dice roll'
I am going to distinguish between two dice by calling them first_dice and second_dice:
first_dice = Dice()
second_dice = Dice()
The Rolls function count the number of equal heads
def Rolls(number):
number_of_equal_sides = 0
for i in range(number):
first_dice.roll()
second_dice.roll()
if (first_dice.top_side == second_dice.top_side):
number_of_equal_sides = number_of_equal_sides + 1
return number_of_equal_sides
I will roll two dices number of times and for each case calculate probability by counting number
of cases where heads are the same and dividing it by the total number of rolls.
number_of_rolls = [100, 1000, 10000, 100000, 1000000]
for number in number_of_rolls:
print "Number of rolls:", number,",","Probability of two sides are the same:", Rolls(number)/float(number)
The larger the number of experiments, the more accurate is probability estimation:
Number of rolls: 100 , Probability of two sides are the same: 0.17
Number of rolls: 1000 , Probability of two sides are the same: 0.147
Number of rolls: 10000 , Probability of two sides are the same: 0.1704
Number of rolls: 100000 , Probability of two sides are the same: 0.1676
Number of rolls: 1000000 , Probability of two sides are the same: 0.167328
Can we prove the answer is correct analytically? If we roll two dice there can only be 6*6 = 36 possible outcomes. Out of 36 outcomes there can only be 6 cases there heads are the same. Therefore 6/36 = 0.16666 is the probability of two dice having the same heads.
I want to try using Monte Carlo method to calculate the probability of having the same heads after rolling two dice.
First I going to define a Class with all possible variable and methods:
import random
class Dice():
def __init__(self):
self.sides = 6
self.top_side = None
def roll(self):
self.top_side = random.randrange(0,self.sides)
def __repr__(self):
return 'Dice roll'
I am going to distinguish between two dice by calling them first_dice and second_dice:
first_dice = Dice()
second_dice = Dice()
The Rolls function count the number of equal heads
def Rolls(number):
number_of_equal_sides = 0
for i in range(number):
first_dice.roll()
second_dice.roll()
if (first_dice.top_side == second_dice.top_side):
number_of_equal_sides = number_of_equal_sides + 1
return number_of_equal_sides
I will roll two dices number of times and for each case calculate probability by counting number
of cases where heads are the same and dividing it by the total number of rolls.
number_of_rolls = [100, 1000, 10000, 100000, 1000000]
for number in number_of_rolls:
print "Number of rolls:", number,",","Probability of two sides are the same:", Rolls(number)/float(number)
The larger the number of experiments, the more accurate is probability estimation:
Number of rolls: 100 , Probability of two sides are the same: 0.17
Number of rolls: 1000 , Probability of two sides are the same: 0.147
Number of rolls: 10000 , Probability of two sides are the same: 0.1704
Number of rolls: 100000 , Probability of two sides are the same: 0.1676
Number of rolls: 1000000 , Probability of two sides are the same: 0.167328
Can we prove the answer is correct analytically? If we roll two dice there can only be 6*6 = 36 possible outcomes. Out of 36 outcomes there can only be 6 cases there heads are the same. Therefore 6/36 = 0.16666 is the probability of two dice having the same heads.






