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) 8 1352
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
On Thu, 20070906 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
Carsten Haese wrote:
On Thu, 20070906 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
On Thu, 20070906 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 sixsided 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
Carsten Haese wrote:
On Thu, 20070906 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 sixsided 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
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, bellcurve (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.
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)
On Sep 6, 1:44 pm, Carsten Haese <cars...@uniqsys.comwrote:
On Thu, 20070906 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 sixsided 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...

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 =...

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...

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...

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:
...

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...

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...

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...

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. ...

by: VivesProcSPL 
last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many Englishlike terms and syntax in an effort to make it easy to learn, particularly for...

by: mar23 
last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...

by: abbasky 
last post by:
### Vandf component communication method one: data sharing
Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...

by: jimatqsi 
last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...

by: isladogs 
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...

by: fareedcanada 
last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...

by: stefan129 
last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multidomain SSL certificates? Any recommendations on reliable providers or specific...

by: davi5007 
last post by:
Hi,
Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

by: MeoLessi9 
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
 