Jump to content

Problem with random module

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
yamman13

yamman13

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
Im sorry to post yet another problem thread, but again I cant seem to solve this on my own.

Im trying out object orientated coding, and so im sure my code isnt optimal, but I dont mind at the moment.
My problem is I cant seem to get the random number function to seed. It doesnt throw up any errors, but I consistently get the same random numbers.


from Tkinter import*
from Canvas import*
from random import*


class main(Frame):
    def __init__(self,master=None):
        Frame.__init__(self,master)
        self.config(bg="dark slate blue")
        self.grid()




        self.box=Canvas(self,bg="burlywood4")
        self.box.grid()


class grass():
    health=100
    energy=5
    age=0
    xpos=randint(0,200)
    ypos=randint(0,200)
    def __init__(self):
        pass


    def growth(self):
            if grass.health>80:
                line=Canvas.create_line(main.box,self.xpos,self.ypos,self.xpos,self.ypos+10,fill="green")
                print self.xpos,self.ypos


print grass.health
main=main()
grass1=grass()
seed()
grass2=grass()
seed()
grass3=grass()

grass1.growth()
grass2.growth()
grass3.growth()

main.mainloop()


#2
spyder

spyder

    Programmer

  • Members
  • PipPipPipPip
  • 120 posts
You need to use the random.seed() function.
Provide it with something that is always different like time.time()
import random

import time

random.seed(time.time())

etc.



#3
yamman13

yamman13

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
I think it uses the system time as the default parameter, and even when I did what you suggested it still didn't seem to reseed the random number.

#4
manux

manux

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 234 posts
Found your bug. Python classes aren't exactly declared like in other languages ;)


You are declaring your instance attributes as follows:
 class grass():
    health=100
    energy=5
    age=0
    xpos=randint(0,200)
    ypos=randint(0,200)
    def __init__(self):
        pass


xpos and ypos are only initialised once. At the creation time of the class.
You need to put this in the __init__ function(that's the constructor), because here you are declaring class variables(instead of instance variables), which are like the equivalent of static class members in C++.

class grass:#no need for parentheses
    def __init__(self):
        self.health=100
        self.energy=5
        self.age=0
        self.xpos=randint(0,200)
        self.ypos=randint(0,200)


And you should only need to seed once.

#5
spyder

spyder

    Programmer

  • Members
  • PipPipPipPip
  • 120 posts
One other thing.
This
...

def growth(self):

            if grass.health>80:

...

should be
...

def growth(self):

            if [B]self[/B].health>80:

...


#6
yamman13

yamman13

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
Thanks everyone, its working fine now.