Op 2006-04-04, Tomi Lindberg schreef <[EMAIL PROTECTED]>: > 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 -- http://mail.python.org/mailman/listinfo/python-list