mercredi 8 avril 2015

Python – random is not actually random

I use this code:



class Car:
def __init__(self):
self.start == [0, roads[0].index('#')]
while roads[self.start[0]][self.start[1]] == '#':
self.start[0] = randint(0, len(roads)-1)
self.start[1] = randint(0, len(roads[0])-1)
self.start = tuple(self.start)

self.finish == [0, roads[0].index('#')]
while roads[self.finish[0]][self.finish[1]] == '#':
self.finish[0] = randint(0, len(roads)-1)
self.finish[1] = randint(0, len(roads[0])-1)
self.finish = tuple(self.finish)

self.kind = randint(0, 1)


What it does is this: in a map represented like this:



#############1#########
#############1111######
#############1##1######
#######1111111111######
#############1#########
#############1#########


it finds two 1s and sets them as start and finish, then it randomly selects one more variable. Then i do this:



a = Car()
b = Car()


only to find out that they both have always the same start and finish, but sometimes different kinds. I don't understand – if there is an issue with random not working, it shouldn't work on self.kind as well!

How do I fix it?





Aucun commentaire:

Enregistrer un commentaire