[Tutor] An Introduction and a question (continuing)
Bob Gailer
bgailer at alum.rpi.edu
Sat Jun 10 00:13:07 CEST 2006
Michael Sullivan wrote:
> OK. I've got it working this far. Now I want the script to generate
> eight pieces, each with a random colour. Here's my current code:
>
> #!/usr/bin/env python
>
> import random
> import time
> import math
>
> class LinePuzzlePiece:
> """This class defines a single playing piece for LinePuzzle"""
> def __init__(self):
> random.seed(time)
> index = int(math.floor(random.uniform(0, 8)))
> colorlist = ["red", "blue", "green", "yellow", "purple", "cyan",
> "orange", "white"]
> self.color = colorlist[index]
>
> def printcolor(self):
> print self.color
>
> piececount = 0
> mypiece = ["", "", "", "", "", "", "", "", ""]
> while (piececount < 9):
> mypiece[piececount] = LinePuzzlePiece()
> mypiece[piececount].printcolor()
> piececount += 1
>
> The problem is that while eight pieces are created and assigned a
> colour, the colour is always the same. I need the colours of the pieces
> to be in a somewhat random order. What am I doing wrong?
>
random.seed(time) sets the seed to the same value each time. (You are
passing a module object, whose ID becomes the seed). Try
random.seed(time.time()). Also you can set the seed once, then let each
call to uniform get the next "random" number.
Also consider:
mypiece = []
for piececount in range(8):
mypiece.append(LinePuzzlePiece())
mypiece[piececount].printcolor()
--
Bob Gailer
510-978-4454
Broadband Phone Service for local and long distance $19.95/mo plus 1 mo Free <http://click.linksynergy.com/fs-bin/click?id=NXXZaiArugE&offerid=86109.10000010&type=3&subid=0>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060609/e0b57ab8/attachment.htm
-------------- next part --------------
A non-text attachment was scrubbed...
Name: show
Type: image/gif
Size: 43 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20060609/e0b57ab8/attachment.gif
More information about the Tutor
mailing list