-2

This is a Monte Carlo Simulation question. Here is my code.

def simulate():
    """
    Simulate three dice and return true if sum is > 10
    """

    die_1 = randint(1,6)
    die_2 = randint(1, 6)
    die_3 = randint(1,6)
    sum = die_1 + die_2 + die_3
    if sum > 10:
      return True
    else:
      return False

The next steps are to use a for loop and call the simulate function to sum up the number of results that end up as True. My idea of doing this:

true_results = 0
for trial in range(1000):
  true_results += simulate()

However, how would I determine what is a True or False result? And does this code make sense?

roTenshi2
  • 57
  • 6
  • Yes this code makes sense. Why don't you try it? Also you can simplify your return logic to single line `return sum > 10`. And similarly you can simplify to `true_results = sum(simulate() for trial in range(1000))` Sumation will implicitely convert `True`/`False` to `0`/`1` – Julien Nov 18 '22 at 04:17

1 Answers1

0

For checking, you can store all summation result to see whether the occurrence, and probability roughly match your expectation.

Your code is fine if it is error-free although the indentation is weird. And i suggest not to use 'sum' as a variable name as it is a build in function name. It still works, but this may get u into trouble in the future.

Ben. S.
  • 91
  • 3