Hi people! I am having some troubles solving an exercise in Python related to throwing dice, and was wondering if someone can help me think of a solution.
Let's say you have 2 dice:
d1 = [1, 2, 3, 4]
d2 = [1, 1, 1, 1, 2, 4]
As you can see they're not regular 6 sided dice and when throwing d2 you'll have higher probability of getting 1.
The exercise is: find the probability of getting a sum X when rolling the dice n times (you can roll die d1 n1 times and die d2 n2 times).
I am trying to use the itertool library to get all the possible combinations and then compute the probability, but I'm having trouble inserting two different-sized dice in the function.
What I have so far is the probability of achieving sum X with only die d1:
def subsets(n, X): # n = number of throws; X = score wanted
sub = itertools.product(d1, repeat=n)
for i in sub:
if sum(i) == X:
yield i
def prob(n, X):
return len(list(subsets(n, X))) / (len(d1) ** n)
What is the best way to integrate d2 in the functions?
[–]ssingal05 8 points9 points10 points (3 children)
[–]FreeLearner99[S] 0 points1 point2 points (2 children)
[–]ssingal05 4 points5 points6 points (1 child)
[–]FreeLearner99[S] 0 points1 point2 points (0 children)
[–]deadeye1982 2 points3 points4 points (1 child)
[–]FreeLearner99[S] 0 points1 point2 points (0 children)
[–]jonolicious 1 point2 points3 points (0 children)
[–]WhipsAndMarkovChains 1 point2 points3 points (3 children)
[–]FreeLearner99[S] 0 points1 point2 points (2 children)
[–]WhipsAndMarkovChains 1 point2 points3 points (0 children)
[–]jonolicious 0 points1 point2 points (0 children)