Python 3中的确定性散列

2024-06-16 12:51:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用字符串散列以以下方式为随机状态设定种子:

context = "string"
seed = hash(context) % 4294967295 # This is necessary to keep the hash within allowed seed values
np.random.seed(seed)

不幸的是(对于我的使用)在Python 3.3和更高版本中的运行之间是不确定的。我知道我可以将PYTHONHASHSEED环境变量设置为一个整数值来重新获得确定性,但我可能更喜欢感觉不那么麻烦的东西,并且不会完全忽略随机散列增加的额外安全性。建议?


Tags: theto字符串stringis状态方式context