Dice probability problem
Antoon Pardon
apardon at forel.vub.ac.be
Tue Apr 4 06:34:18 EDT 2006
Op 2006-04-04, Tomi Lindberg schreef <tomi.lindberg.NO_SPAM at pp.inet.fi.invalid>:
> Hi,
>
> I'm trying to find a way to calculate a distribution of
> outcomes with any combination of dice. I have the basics
> done, but I'm a bit unsure how to continue. My main concern
> is how to make this accept any number of dice, without
> having to write a new list comprehension for each case?
IMO you are looking at it from the wrong side.
It would be better to construct distributions for one
die and make a function that can 'add' two distributions
together. So for 3D6 you first add the distribution of
a D6 to the distribution of a D6 and to this result
you add the distribution of a D6 again.
If you need more to start, just ask.
> Here's a piece of code that shows the way I'm doing things
> at the moment.
>
> -- code begins --
>
> # A die with n faces
> D = lambda n: [x+1 for x in range(n)]
>
> # A pool of 3 dice with 6 faces each
> pool = [D(6)] * 3
>
> # A List of all outcomes with the current 3d6 pool.
> results = [x+y+z for x in pool[0] for y in pool[1]
> for z in pool[2]]
This is very inefficient. I wouldn't want to calculate
the distribution of 10D10 this way.
Try to think how you would do this with only D2's.
(Triangle of Pascal) and generalize it.
--
Antoon Pardon
More information about the Python-list
mailing list