Prechádzať zdrojové kódy

Fix seeding the program with a string.

dustin.born 6 rokov pred
rodič
commit
d4d8d833fc
1 zmenil súbory, kde vykonal 5 pridanie a 3 odobranie
  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)