Beginners Query - Simple counter problem
mensanator at aol.com
mensanator at aol.com
Thu Sep 6 19:39:44 EDT 2007
On Sep 6, 1:44 pm, Carsten Haese <cars... at uniqsys.com> wrote:
> On Thu, 2007-09-06 at 11:24 -0700, Ian Clark wrote:
> > Carsten Haese wrote:
> > > def d6(count):
> > > return sum(random.randint(1, 6) for die in range(count))
>
> > My stab at it:
>
> > >>> def roll(times=1, sides=6):
> > ... return random.randint(times, times*sides)
>
> That produces an entirely different probability distribution if times>1.
> Consider times=2, sides=6. Your example will produce every number
> between 2 and 12 uniformly with the same probability, 1 in 11. When
> rolling two six-sided dice, the results are not evenly distributed. E.g.
> the probability of getting a 2 is only 1 in 36, but the probability of
> getting a 7 is 1 in 6.
>
> --
> Carsten Haesehttp://informixdb.sourceforge.net
Why settle for a normal distribution?
import random
def devildice(dice):
return sum([random.choice(die) for die in dice])
hist = {}
for n in xrange(10000):
the_key = devildice([[1,2,3,10,11,12],[4,5,6,7,8,9]])
if the_key in hist:
hist[the_key] += 1
else:
hist[the_key] = 1
hkey = hist.keys()
m = max(hkey)
n = min(hkey)
histogram = [(i,hist.get(i,0)) for i in xrange(n,m+1)]
for h in histogram:
print '%3d %s' % (h[0],'*'*(h[1]/100))
## 5 **
## 6 *****
## 7 ********
## 8 ********
## 9 ********
## 10 *******
## 11 *****
## 12 **
## 13
## 14 **
## 15 ******
## 16 ********
## 17 ********
## 18 ********
## 19 ********
## 20 *****
## 21 **
They're called Devil Dice because the mean is 13 even
though you cannot roll a 13.
More information about the Python-list
mailing list