瀏覽代碼

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 6 年之前
父節點
當前提交
15cbf29fd5
共有 1 個文件被更改,包括 13 次插入3 次删除
  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):
         """