Paired dice simulation
Similar to the example in the lesson, you will roll two dice from two bags, and each bag contains three biased dice.
bag1 = [[1, 2, 3, 6, 6, 6], [1, 2, 3, 4, 4, 6], [1, 2, 3, 3, 3, 5]]
bag2 = [[2, 2, 3, 4, 5, 6], [3, 3, 3, 4, 4, 5], [1, 1, 2, 4, 5, 5]]
The difference is that the dice in the two bags are paired: if you pick the second die in bag1
, you will also pick the second die in bag2
. In each trial:
- You pick one pair of dice from the two bags randomly and roll them
- Success occurs if the points on
dice1
anddice2
add up to eight; otherwise, failure
Your task is to complete the for-loop in the roll_paired_biased_dice()
function and to use this function to calculate the probabilities of success for each unique combination of points on dice1
and dice2
.
The following have been imported for you: random
, numpy
as np
, pandas
as pd
, seaborn
as sns
and matplotlib.pyplot
as plt
.
This exercise is part of the course
Monte Carlo Simulations in Python
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
def roll_paired_biased_dice(n, seed=1231):
random.seed(seed)
results={}
for i in range(n):
bag_index = random.randint(0, 1)
# Obtain the dice indices
dice_index1 = ____
dice_index2 = ____
# Sample a pair of dice from bag1 and bag2
point1 = ____
point2 = ____
key = "%s_%s" % (point1,point2)
if point1 + point2 == 8:
if key not in results:
results[key] = 1
else:
results[key] += 1
return(pd.DataFrame.from_dict({'dice1_dice2':results.keys(),
'probability_of_success':np.array(list(results.values()))*100.0/n}))