86.2k views
3 votes
Write a program that performs a simulation to estimate the probability of rolling five of a kind in a single toss of five six-sided dice. Show your results when running 100 Monte Carlo trials, 10,000 Monte Carlo trials, and 1,000,000 Monte Carlo trials.Signature:probability_five_of_a_kind(num_trials)return typestring (formatted to show eight decimal places)Test case:>>> pro bability_five_of_a_kind(500_000)'The probability_five_of_a_kind is 0.00074000'

User Yehia Awad
by
5.5k points

1 Answer

2 votes

Answer:

import random

def probability_five_of_a_kind(num_trials):

sums = 0

for _ in range(num_trials):

roll_1 = random.randrange(1,6, 1)

roll_2 = random.randrange(1,6, 1)

roll_3 = random.randrange(1,6, 1)

roll_4 = random.randrange(1,6, 1)

roll_5 = random.randrange(1,6, 1)

collection = roll_1 + roll_2 + roll_3 + roll_4 + roll_5

if collection == 5:

sums += 1

prob = round(sums/7776, 8)

print(f"The probability_five_of_a_kind is {prob}")

probability_five_of_a_kind(100)

probability_five_of_a_kind(10000)

probability_five_of_a_kind(1000000)

Step-by-step explanation:

The random package of the python language is used to select an integer value from the range of one to six representing the sides of a die. All six rolls are randomized and the sum. If the sum is equal to 5, the sums counter is incremented. At the end of the loop, the sum is divided by the five dices events (which is 6^5 = 7776).

User Elya
by
5.9k points