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 1374
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***********@A cm.Org
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.rand int(1, 6) for die in range(count))
--
Carsten Haese http://informixdb.sourceforge.net
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.rand int(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, 2007-09-06 at 11:24 -0700, Ian Clark wrote:
Carsten Haese wrote:
def d6(count):
return sum(random.rand int(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
Carsten Haese wrote:
On Thu, 2007-09-06 at 11:24 -0700, Ian Clark wrote:
>Carsten Haese wrote:
>>def d6(count): return sum(random.rand int(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
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***********@A cm.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.
On Sep 6, 10:29 am, David Barr <david.barr...@ btinternet.comw rote:
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...@uniqsy s.comwrote:
On Thu, 2007-09-06 at 11:24 -0700, Ian Clark wrote:
Carsten Haese wrote:
def d6(count):
return sum(random.rand int(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.sour ceforge.net
Why settle for a normal distribution?
import random
def devildice(dice) :
return sum([random.choice(d ie) 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 2003 Server or ADO or ODBC issue,
I am posting this on all of the three newsgroups.
That's the setup:
Windows 2003 Server with IIS and ASP.NET actiavted
Access 2002 mdb file (and yes, proper rights are set on TMP paths and path,
|
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 = 1234
Now, let's write a simple VB program to do this query back to an
MSDE/2000 database on our local machine. Effectively, we'll
|
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 select query that shows both fields, is
|
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 am doing wrong? I
am I right in saying that In assuming that I dont to loop since I am
returnign this for a every record in query.
Your help will be greatly appreciated.
Function Generate_Number(emp_no As Variant) As Variant
Dim strSQL As...
|
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:
"Date","P1","P2","P3","P4","P5","P6","P7","P8","P9","P10","P11","P12","P13","P14","P15","P16","P17","P18","P19","P20","P21"
1/1/2005,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21
1/2/2005,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22
| |
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 luke_smith
{
/// <summary>
/// Summary description for counter.
|
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 was the following:
Take the query that I am executing, copy the query and turn it into a count
query, run the count query, then execute the original query.
The reason for this is so that I can implememt public paging on my website.
The...
|
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 has time to critic and offer
my some feedback I'd be grateful
Thanks
Joel
|
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. (i.e.
"Addhandler application/x-httpd-php .html .php" in .htaccess). He
says it is a security problem.
Is it possible to make a PHP counter WITHOUT using echo command?
Thanks for your suggestions,
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |