简体   繁体   中英

How to generate random number by given string in python?

As hash function gives different values in different python interpreters , Is there a more stable one in python ?

Update one:

"stable" means that the function should give the same number when the string is same anytime. hash can't do it .

I want a function like :

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

For example , generate_random_number_by_string("16") should be the same in anytime .

You can use a hash from hashlib . For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM