Quellcode durchsuchen

Fix seeding and add seeding with strings

Before, the seeding did not work when the user specified a seed
via parameter. This is because the seed was never an int but a
string when entered as ID2T parameter.

Now: The original functionality persists, i.e. if the seed comes
as int, it is processed as int. If the seed is a string, but
contains only digits, it is converted to an int. Finally, if the
seed is a string that contains non-digit characters, the string
is hashed to an int, which is then used as seed.
dustin.born vor 6 Jahren
Ursprung
Commit
15cbf29fd5
1 geänderte Dateien mit 13 neuen und 3 gelöschten Zeilen
  1. 13 3
      code/Attack/BaseAttack.py

+ 13 - 3
code/Attack/BaseAttack.py

@@ -286,13 +286,23 @@ class BaseAttack(metaclass=abc.ABCMeta):
     #########################################
 
     @staticmethod
-    def set_seed(seed: int):
+    def set_seed(seed):
         """
         :param seed: The random seed to be set.
         """
+        seed_final = None
         if isinstance(seed, int):
-            random.seed(seed)
-            np.random.seed(seed)
+            seed_final = seed
+        elif isinstance(seed, str):
+            if seed.isdigit():
+                seed_final = int(seed)
+            else:
+                hashed_seed = hashlib.sha1(seed.encode()).digest()
+                seed_final = int.from_bytes(hashed_seed, byteorder="little")
+
+        if seed_final:
+            random.seed(seed_final)
+            np.random.seed(seed_final & 0xFFFFFFFF)
 
     def set_start_time(self):
         """