Skip to content

Instantly share code, notes, and snippets.

@vicenterocha
Last active February 4, 2019 21:18
Show Gist options
  • Save vicenterocha/25ea083a1b1d41c9e7c3b6edb86c51ac to your computer and use it in GitHub Desktop.
Save vicenterocha/25ea083a1b1d41c9e7c3b6edb86c51ac to your computer and use it in GitHub Desktop.
'''
Given 3 fair six-sided dice, on a random roll, what is the probability that the sum of their values adds up to k=10 or more
3 dados de 6 lados
prob 1 3 6
'''
import random
def calculate_prob_m_dice_sum_greater_than_n(m=3, n=1000000, t=10):
"""
Calculates the probability of the sum of m dice to be greater than t.
It performs the experiment n times.
:param m: number of dice
:param n: number of experiments
:param t: threshold for count
:return: returns the probability
"""
count = 0
# simulate n experiments
for _ in range(n):
dice_sum = 0
# throw m dice
for _ in range(m):
# simulate rolled dice number
rolled_num = random.randint(1, 6)
dice_sum += rolled_num
# if sum is greater than t increase count
if dice_sum >= t:
count += 1
return count / n
prob = calculate_prob_m_dice_sum_greater_than_n()
print(prob)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment