473,320 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Beginners Query - Simple counter problem

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)
Sep 6 '07 #1
8 1360
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
Sc***********@Acm.Org
Sep 6 '07 #2
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
Sep 6 '07 #3
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

Sep 6 '07 #4
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
Sep 6 '07 #5
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

Sep 6 '07 #6
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
Sc***********@Acm.Org
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.
Sep 6 '07 #7
On Sep 6, 10:29 am, David Barr <david.barr...@btinternet.comwrote:
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)

Sep 6 '07 #8
On Sep 6, 1:44 pm, Carsten Haese <cars...@uniqsys.comwrote:
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.

Sep 6 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Wolfgang Kaml | last post by:
Hello All, I have been working on this for almost a week now and I haven't anything up my sleeves anymore that I could test in addition or change.... Since I am not sure, if this is a Windows...
3
by: Andrew Mayo | last post by:
There is something very strange going on here. Tested with ADO 2.7 and MSDE/2000. At first, things look quite sensible. You have a simple SQL query, let's say select * from mytab where col1 =...
2
by: David Gill | last post by:
Hi I have the following problem, which I am sure is easy to resolve for many Access users. A table contains the following fields: 1) Student_Id 2) Results_Id Problem : I wish to create a...
2
by: Anderson | last post by:
I have a table which has employee number. I have attempted to creat a function whic will derive a unique number for each record how ever the code below only returns 10,000 for all records. What I...
8
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: ...
1
by: Luke Smith | last post by:
i have the following code in counter.cs but i get errors regarding request, application, session. I know im missing something but dont know what it is. thnx using System.IO; namespace...
4
by: Chris Tremblay | last post by:
I am trying to figure out how to go about retrieving the number of results returned from my queries in SQL server from VB.NET without using a the Select Count(*) query. The method that I was using...
10
by: Joel Mayes | last post by:
Hi All; I'm teaching myself C, and have written a prime number generator. It is a pretty inefficient implementation of the Sieve of Eratosthenes to calculate primes up to 1,000,000. If anyone...
9
by: Pygmalion | last post by:
I have found dozen of useful PHP counters on the web. However, nobody is working for my web pages, since administrator does not want to enable the possibility that PHP could be called from HTML. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.