Ver Fonte

Fix seeding the program with a string.

dustin.born há 6 anos atrás
pai
commit
d4d8d833fc
1 ficheiros alterados com 5 adições e 3 exclusões
  1. 5 3
      code/CLI.py

+ 5 - 3
code/CLI.py

@@ -3,6 +3,7 @@ import argparse
 import sys
 import random
 import numpy
+import hashlib
 
 from ID2TLib.Controller import Controller
 
@@ -92,9 +93,10 @@ class CLI(object):
     def seed_rng(self, seed):
         try: # try to convert the seed to int
             seed = int(seed)
-        except:
-            seed = hash(seed) # otherwise use the strings hash
-        
+        except: # otherwise use the strings hash
+            hashed_seed = hashlib.sha1(seed.encode()).digest()
+            seed = int.from_bytes(hashed_seed, byteorder="little") & 0xffffffff  # convert hash to 32-bit integer
+
         random.seed(seed)
         numpy.random.seed(seed)