From 16d16c7559682c8f0b6887ac378f4f5aa49b51cc Mon Sep 17 00:00:00 2001 From: Peter Vacho <p_vacho@utb.cz> Date: Sun, 16 Mar 2025 22:02:41 +0100 Subject: [PATCH] Use consistent hashing for rng seeding Turns out the built-in `hash()` function in python doesn't behave consistently across re-runs, and while it does return the same value during the same program run, once the interpreter is closed and a new instance is started, hash behaves differently. This inconsistency is unacceptable, as the whole point of the seed is to be able to achieve the same results consistently. --- src/ui.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ui.py b/src/ui.py index 2cc5b04..63fb8bb 100644 --- a/src/ui.py +++ b/src/ui.py @@ -1,4 +1,5 @@ from typing import TypedDict +import hashlib import matplotlib.pyplot as plt import numpy as np @@ -127,7 +128,8 @@ class OptimizationUI(QWidget): seed = self.seed_input.text() if seed != "": - bit_gen = np.random.PCG64(abs(hash(seed))) + seed_int = int.from_bytes(hashlib.sha256(seed.encode("utf-8")).digest()) + bit_gen = np.random.PCG64(seed_int) rng = np.random.Generator(bit_gen) else: rng = None