0

I want to understand how I can control the "random seed" that's used in set.pop to select the arbitrary element to be popped.

Consider this example:

>>> for _ in range(5):
...   x = {b'a', b'b', b'c'}
...   print(x.pop())
... 
b'a'
b'a'
b'a'
b'a'
b'a'

It seems as though the process for popping an element from the x set is deterministic. However, there is something set under the hood that fixes this deterministic behaviour. If I run this from another terminal (i.e. another python session), I get a different, but still deterministic result:

>>> for _ in range(5):
...   x = {b'a', b'b', b'c'}
...   print(x.pop())
... 
b'c'
b'c'
b'c'
b'c'
b'c'

How can I have reproducibility across different python processes for the set.pop method?

Vladimir Vargas
  • 1,744
  • 4
  • 24
  • 48
  • Does this answer your question? [Set.pop() isn't random?](https://stackoverflow.com/questions/21017188/set-pop-isnt-random) – pjs Apr 20 '23 at 17:34
  • It does not answer my question, as the answer in the question you link @pjs does not show how to have reproducibility across different python processes. – Vladimir Vargas Apr 20 '23 at 18:02
  • The linked Q&A tell you that it's not a random process, so PRNG seeds have nothing to do with it. One of the answers points out that the use of "arbitrary" in the spec means the implementation can do whatever it wants, which in turn implies that the built-in `set` behavior is not controllable by you. – pjs Apr 20 '23 at 18:27
  • I highly doubt that the arbitrariness cannot be controlled by me. I wonder if this has to do with the set being a hashmap, and this it uses the internal `hash` Python function, which is also arbitrary across Python sessions – Vladimir Vargas Apr 21 '23 at 10:48
  • 1
    @VladimirVargas: You may be looking for the `PYTHONHASHSEED` environment variable: https://docs.python.org/3/using/cmdline.html#envvar-PYTHONHASHSEED – Mark Dickinson Apr 24 '23 at 11:43
  • @MarkDickinson could you expand your comment into an answer so that I can accept it? – Vladimir Vargas Apr 26 '23 at 13:26

0 Answers0