Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3中的确定性哈希_Python_Python 3.x_Hash - Fatal编程技术网

Python 3中的确定性哈希

Python 3中的确定性哈希,python,python-3.x,hash,Python,Python 3.x,Hash,我使用字符串散列以以下方式播种随机状态: context = "string" seed = hash(context) % 4294967295 # This is necessary to keep the hash within allowed seed values np.random.seed(seed) 不幸的是(在我的使用中)在Python3.3和更高版本中的运行之间是不确定的。我确实知道我可以将PYTHONHASHSEED环境变量设置为整数值以恢复确定性,但我可能更喜欢感觉不那

我使用字符串散列以以下方式播种随机状态:

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

不幸的是(在我的使用中)在Python3.3和更高版本中的运行之间是不确定的。我确实知道我可以将
PYTHONHASHSEED
环境变量设置为整数值以恢复确定性,但我可能更喜欢感觉不那么刺耳的东西,并且不会完全忽略随机哈希增加的额外安全性。建议?

使用专门构建的哈希函数。是一个很好的选择;或者,请查看模块以获得更多选项。

强制Python的内置
哈希
为确定性本质上是有问题的。如果要避免hackized,请使用不同的散列函数——例如在Python-2中:,
在Python-3中:

散列不应该是确定性的吗?hash()只在同一次运行中是确定性的,不能保证它在不同的运行中返回相同的散列。因此,它不利于磁盘上的持久性。小心!我很难找到答案,但是adler32的目的不是为了散列,而是为了纠错。它有相当高的碰撞概率。调试起来相当头疼。但是目的是什么?为什么不简单地编写
seed=42
,除非您确实希望seed在不同的运行中有所不同?