Michael Sullivan wrote:
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.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? 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 |
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor