Let's roll one dice two times, and ask some questions about our rolls. In this case, let's see what the probability as a whole is, of getting 4, 5, or 6 on the first roll, and a 1, 2, 3, or 4 on the second roll.
As in last lessons, we'll set N equal to the number of rolls we'd like to make. Next, we'll set up a "roll for-loop" for each roll. The first one study what the probability is to roll a 4, 5, or 6 on dice #1. The second will look for the probability of rolling a 1, 2, 3. A dice roll will be simulated with math.random(1,6).
Note here that we have two separate results, d1 and d2. d1 will be the chance of rolling a 4, 5, or 6. d2 will be the chance of rolling a 1, 2, 3, or 4.
Note a key word in the question, the "and": "getting a 4, 5, or 6 on the first roll and a 1, 2, 3, or 4 on the second roll. "And" in probability means to multiply any separate and independent probabilities, so in the end, we multiply d1 and d2 to find the final answer.
Now you try. Run as is, or change up the numbers to look for on the rolls.
Type your code here:
See your results here:
We can apply some common sense to verify the computer's answer. Keep in mind, for a fair dice, each number is equally likely to show after a roll. Thus, each number has a 1 in 6 chance of showing, or a probability of $1/6$.
The chance of rolling a 4, 5 or 6, is $3\times 1/6$ or $3/6$ or $1/2$ or $0.5$. So, d1 in the above code should result in around this number. For large values of N, like $1,000$ or so, $0.5$ should result.
The chance of rolling a 1, 2, 3 or 4 us $4\times 1/6$ or $4/6$ or $2/3$ or 0.66. So, d2 in the above code should result in around this number. For large values of N, like 1,000 or so, $0.66$ should result. Note the repeated look for a "4" between the two rolls has nothing to do with the fact that the dice is "fair" and all numbers have a 1 in 6 chance of coming up.
Lastly, we address the "and" by multiplying d1 and d2 for the final answer to our question.
Share your code
Show a friend, family member, or teacher what you've done!