This occurs because random.sample(s, 1)
calls list(s)
internally, thus flattening the set into a list in a nondeterministic order. This occurs before trying to use the random.random()
function. The problem with writing a doctest is the same as writing a doctest to check a set: you can't, so you need workarounds like checking sorted(s)
.
In the simplest cases you can solve it by calling random.sample(sorted(s), 1)
. If the code is more involved and it doesn't make sense to add sorted()
there in production, all I can say is good luck...