CoCalc Public FilesProject / S21 / Final Project Draft / Code For project 4.ipynbOpen with one click!
Authors: Johann Thiel, C Y
Views : 66
Compute Environment: Ubuntu 20.04 (Default)

Dice Roll Game

In this game, four six-sided die is being rolled. What is the probability of where all the dice can be paired off so that one pair adds up to four and the other pair adds up to nine?

First, we have to import numpy in to allow the simulation to work
In [1]:
import numpy as np
Next, we have to create four dice for the dice rolls
In [2]:
D1= np.random.randint(1,7) D2=np.random.randint(1,7) D3=np.random.randint(1,7) D4=np.random.randint(1,7)
Then, we have to list all wanted results. Which is basically all possible set of pairs, and each set has two wanted result where the first pair adds up to 7 and the second pair adds up to 9, or vice-versa. And which we got:
(D1+D2==7 and D3+D4==9) or (D1+D2 ==9 and D3+D4==7)
(D2+D3==7 and D1+D4==9) or (D2+D3 ==9 and D1+D4==7)
(D2+D4==7 and D1+D3==9) or (D2+D4 ==9 and D1+D3==7)
After that, we can make the code that runs the simulation multiple time and when there is a run that has one of the wanted result, it return a 1, likewise, it returns 0.
In [3]:
def dice_rolls(): D1= np.random.randint(1,7) D2=np.random.randint(1,7) D3=np.random.randint(1,7) D4=np.random.randint(1,7) if (D1+D2==7 and D3+D4==9) or (D1+D2 ==9 and D3+D4==7): return 1 elif (D2+D3==7 and D1+D4==9) or (D2+D3 ==9 and D1+D4==7): return 1 elif (D2+D4==7 and D1+D3==9) or (D2+D4 ==9 and D1+D3==7): return 1 else: return 0
Lastly, we can run the simulation multiple times, and add up all the 1 and divide it by the total run to obtain the probabilty. Which in this case we run it 10000 times
In [5]:
sum([dice_rolls() for i in range(10000)])/10000
0.0778
Our simulation suggest that if we were to roll four dice, the probabilty of getting a result where one pair adds up to seven and the other pair adds up to nine is 7.78%
In [ ]: