1

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?

1 Answers1

0

If you're using the random module, you can force it to always generate the same sequence of pseudo-random values by initializing it with a call to the function random.seed() with a specific value.

I generally use random.seed(42), but some other value would be fine.

This is useful because it often makes testing easier.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
martineau
  • 119,623
  • 25
  • 170
  • 301