I was debugging python code and astonished to find that it didn't always produce the same output. Specifically, at some point I choose a random element from a set by turning it into a list and taking the first element and I don't always get the same element.
This code illustrates the behaviour.
class Foo():
def __init__(self,n):
self.n = n
}
def __repr__(self):
return "<{}>".format(self.n)
print(list(set(Foo(i) for i in (1,2,3))))'''
It sometimes produces [<3>, <1>, <2>] and sometimes [<1>, <2>, <3>]
I have tried calling random.seed(0) before execution but this has no effect
It's not relevant but in my actual problem I'm separating a graph into connected components by repeatedly choosing an unprocessed node and flood-filling from that node. My code chooses random nodes to initiate the process so the components are calculated in a different order and this makes debugging the code more difficult, the working and the order of the components can vary.
I'm aware that I could force reproducibility by sorting the list of nodes before choosing the first in the list but this introduces an unwanted overhead. Am I naive in thinking a basic tenet of a computer language is that it should always behave the same on the same input?