jeudi 12 décembre 2019

Why are random generated sequences of numbers the same for different class instances?

I've been trying to generate different dictionary structures (pertaining to different class instances in Python) filled with random number values between [0, 1). The keys of the dictionary are not that the important. But, the main problem is that when I try and generate the dictionaries with the random values, they all come up as the same sequence:

{'p0': 0.8834439890229875, 'p1': 0.4542011548977558, 'd0': 0.041855079212439805, 'c0': 0.30179244567633823, 'c1': 0.026356543619428408, 'c2': 0.24603169392476631}
{'p0': 0.8834439890229875, 'p1': 0.4542011548977558, 'd0': 0.041855079212439805, 'c0': 0.30179244567633823, 'c1': 0.026356543619428408, 'c2': 0.24603169392476631}
{'p0': 0.8834439890229875, 'p1': 0.4542011548977558, 'd0': 0.041855079212439805, 'c0': 0.30179244567633823, 'c1': 0.026356543619428408, 'c2': 0.24603169392476631}

Code:

''' Individual Class '''

from random import Random

class Individual:

        chromosome = {} #The chromosome of an individual is a random generated dictionary.
        randomInstance = Random(datetime.now())

        def __init__(self, numP, numD, numC):
                self.randomInstance.seed()
                for i in range(numP):
                        plant = "p"
                        plant += str(i)
                        self.chromosome[plant] = self.randomInstance.random()

                for j in range(numD):
                        plant = "d"
                        plant += str(j)
                        self.chromosome[plant] = self.randomInstance.random()

                for k in range(numC):
                        plant = "c"
                        plant += str(k)
                        self.chromosome[plant] = self.randomInstance.random()

def main():

        list = []
        for i in range(4):
                inst = Individual(2, 1 ,3)
                list.append(inst.chromosome)

        print(list)

I'm trying to get a different sequence per class instance.

Hope someone can help. Thank you all.




Aucun commentaire:

Enregistrer un commentaire