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

标签 python python-3.x

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

更新一:

“stable”表示当字符串在任何时候都相同时,函数应该给出相同的数字。 hash 做不到。

我想要一个类似的功能:

def generate_random_number_by_string(string:str)->int:
    pass

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

最佳答案

您可以使用 hashlib 中的哈希。例如:

import hashlib
int(hashlib.sha256(b'yourstring').hexdigest(), base=16)

关于python - 如何通过python中的给定字符串生成随机数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64474687/

相关文章:

python - _curses.error : add_wch() returned an error

Python 类型错误 : can only concatenate list (not "int") to list for Parkes error grid

python-3.x - botocore.exceptions.ProfileNotFound : The config profile (default) could not be found

python - 我可以在同一台 Windows 计算机上安装 Python 3.x 和 2.x 吗?

python - 为什么像 01 这样的数字在 python 交互模式下会出现语法错误

python - Pandas - 合并/加入/vlookup df 并删除所有匹配的行

python - 打印 "find_all"(使用 bs4 库)的结果返回一个空列表(但我引用的类存在)

python - 如何访问图形工具边缘?为什么图形工具边没有 id?

python - 如何获得小时 :minutes

python - 使用 celery 任务创建新进程