Here is function to count the number of elementary events for sum of multiple throws of n-faced dice.

# Counting elementary events for sum of variable number of n-faced dice

```
def find_sums(num_dice, num_faces):
""" Given the number of dice with a given number of faces (numbered 1...num_faces),
returns a dictionary mapping all the obtainable sums (elementary events) to the number of ways to
get the sum for one throw of the dice. """
# first die
sums = dict([(i, 1) for i in range(1, num_faces + 1)])
# rest of the dice
for die in range(2, num_dice + 1):
new_sums = {}
# take each existing sum
for (total, count) in sums.items():
# and add each possible die face to it, generating new sums
for face in range(1, num_faces + 1):
temp = total + face # new sum
if temp in new_sums:
new_sums[temp] = new_sums[temp] + count
else:
new_sums[temp] = count
sums = new_sums
return sums
```

Gribouillis
1,391
Programming Explorer
Team Colleague

TrustyTony
commented:
You show that sometimes it is usefull to know some maths
+13

TrustyTony
888
pyMod
Team Colleague
Featured Poster

TrustyTony
888
pyMod
Team Colleague
Featured Poster

TrustyTony
888
pyMod
Team Colleague
Featured Poster

TrustyTony
888
pyMod
Team Colleague
Featured Poster

TrustyTony
888
pyMod
Team Colleague
Featured Poster

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.