如何通过python中的给定字符串生成随机数?

Dac*_*hao 2 python python-3.x

由于hash函数在不同的 python 解释器中给出不同的值,python 中有更稳定的函数吗?

更新一:

“稳定”意味着该函数应该在任何时候字符串相同时给出相同的数字。hash做不到。

我想要一个像这样的功能:

def generate_random_number_by_string(string:str)->int:
    pass
Run Code Online (Sandbox Code Playgroud)

例如,generate_random_number_by_string("16")在任何时候都应该是一样的。

Cor*_*zin 6

您可以使用来自hashlib. 例如:

import hashlib
int(hashlib.sha256(b'yourstring').hexdigest(), base=16)
Run Code Online (Sandbox Code Playgroud)