Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Beginners Query - Simple counter problem

Reply
Thread Tools

Beginners Query - Simple counter problem

 
 
David Barr
Guest
Posts: n/a
 
      09-06-2007
I am brand new to Python (this is my second day), and the only
experience I have with programming was with VBA. Anyway, I'm posting
this to see if anyone would be kind enough to help me with this (I
suspect, very easy to solve) query.

The following code is in a file which I am running through the
interpreter with the execfile command, yet it yeilds no results. I
appreciate I am obviously doing something really stupid here, but I
can't find it. Any help appreciated.


def d6(i):
roll = 0
count = 0
while count <= i:
roll = roll + random.randint(1,6)
count += 1

return roll

print d6(3)
 
Reply With Quote
 
 
 
 
Scott David Daniels
Guest
Posts: n/a
 
      09-06-2007
David Barr wrote:
> I am brand new to Python (this is my second day), and the only
> experience I have with programming was with VBA. Anyway, I'm posting
> this to see if anyone would be kind enough to help me with this (I
> suspect, very easy to solve) query.
>
> The following code is in a file which I am running through the
> interpreter with the execfile command, yet it yeilds no results. I
> appreciate I am obviously doing something really stupid here, but I
> can't find it. Any help appreciated.
>
>
> def d6(i):
> roll = 0
> count = 0
> while count <= i:
> roll = roll + random.randint(1,6)
> count += 1
>
> return roll
>
> print d6(3)

A) your direct answer: by using <=, you are rolling 4 dice, not 3.
B) Much more pythonic:

import random

def d6(count):
result = 0
for die in range(count):
result += random.randint(1, 6)
return result

-Scott David Daniels
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Carsten Haese
Guest
Posts: n/a
 
      09-06-2007
On Thu, 2007-09-06 at 11:00 -0700, Scott David Daniels wrote:
> def d6(count):
> result = 0
> for die in range(count):
> result += random.randint(1, 6)
> return result


This, of course, can be further improved into:

def d6(count):
return sum(random.randint(1, 6) for die in range(count))

--
Carsten Haese
http://informixdb.sourceforge.net


 
Reply With Quote
 
Ian Clark
Guest
Posts: n/a
 
      09-06-2007
Carsten Haese wrote:
> On Thu, 2007-09-06 at 11:00 -0700, Scott David Daniels wrote:
>> def d6(count):
>> result = 0
>> for die in range(count):
>> result += random.randint(1, 6)
>> return result

>
> This, of course, can be further improved into:
>
> 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)

Ian

 
Reply With Quote
 
Carsten Haese
Guest
Posts: n/a
 
      09-06-2007
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 Haese
http://informixdb.sourceforge.net


 
Reply With Quote
 
Ian Clark
Guest
Posts: n/a
 
      09-06-2007
Carsten Haese 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.
>


Doh. I stand corrected. Probability was never a fun subject for me.

Ian

 
Reply With Quote
 
David Barr
Guest
Posts: n/a
 
      09-06-2007
Scott David Daniels wrote:
> David Barr wrote:
>> I am brand new to Python (this is my second day), and the only
>> experience I have with programming was with VBA. Anyway, I'm posting
>> this to see if anyone would be kind enough to help me with this (I
>> suspect, very easy to solve) query.
>>
>> The following code is in a file which I am running through the
>> interpreter with the execfile command, yet it yeilds no results. I
>> appreciate I am obviously doing something really stupid here, but I
>> can't find it. Any help appreciated.
>>
>>
>> def d6(i):
>> roll = 0
>> count = 0
>> while count <= i:
>> roll = roll + random.randint(1,6)
>> count += 1
>>
>> return roll
>>
>> print d6(3)

> A) your direct answer: by using <=, you are rolling 4 dice, not 3.
> B) Much more pythonic:
>
> import random
>
> def d6(count):
> result = 0
> for die in range(count):
> result += random.randint(1, 6)
> return result
>
> -Scott David Daniels
> (E-Mail Removed)


I was surprised by the speed and number of posts. Thanks for the
solutions provided!

>>> def roll(times=1, sides=6):

.... return random.randint(times, times*sides)

Although this would probably be quicker than the other approaches, I'm
not using the dice to generate numbers per say, I actually want to
emulate the rolling of dice, bell-curve (normal distribution) as well as
the range.

Thanks again, I already like what (very) little I can do in Python and
it seems to have a great community too.

Cheers,
Dave.
 
Reply With Quote
 
Zentrader
Guest
Posts: n/a
 
      09-06-2007
On Sep 6, 10:29 am, David Barr <(E-Mail Removed)> wrote:
> yields no results.


Since every response so far has answered everything __Except The
Question You Asked__, your code runs fine on my Linux machine and
prints 15. The error may be before this bit of code so it isn't
getting called. Add some print statements and try again

def d6(i):
print "start of d6()"
roll = 0
count = 0
while count <= i:
print "d6 count =", count, "of", i
roll = roll + random.randint(1,6)
count += 1

print "returning roll =", roll
return roll

print d6(3)

 
Reply With Quote
 
mensanator@aol.com
Guest
Posts: n/a
 
      09-06-2007
On Sep 6, 1:44 pm, Carsten Haese <(E-Mail Removed)> 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.

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      09-07-2007
On Thu, 06 Sep 2007 18:29:32 +0100, David Barr
<(E-Mail Removed)> declaimed the following in
comp.lang.python:

> The following code is in a file which I am running through the
> interpreter with the execfile command, yet it yeilds no results. I
> appreciate I am obviously doing something really stupid here, but I
> can't find it. Any help appreciated.
>

Many responses on coding the d6 function... But I have to ask: WHY
are you using execfile!
--
Wulfraed Dennis Lee Bieber KD6MOG
(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple drivers problem with a simple set-enable counter process Maxim VHDL 7 03-09-2010 12:36 AM
Page File counter and Private Bytes Counter George2 C++ 1 01-31-2008 09:27 AM
Beginners Query j.vinothkr@gmail.com Perl Misc 1 05-12-2005 10:10 AM
Stat Counter Query KiwiBrian HTML 1 04-16-2005 02:49 PM
Session("counter") vs. ViewState("counter")...a newbie question The Eeediot ASP .Net 3 12-22-2004 09:31 PM



Advertisments