4

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 .

2
  • Can you define "stable"? Why does this matter to your application? Commented Oct 22, 2020 at 3:10
  • Hi , @ggorlen . "stable" means that the function should give the same number when the string is same anytime. hash can't do it . Commented Oct 22, 2020 at 3:19

1 Answer 1

7

You can use a hash from hashlib. For example:

import hashlib
int(hashlib.sha256(b'yourstring').hexdigest(), base=16)
Sign up to request clarification or add additional context in comments.

3 Comments

@DachuanZhao Yes, sha256 hash gives totally uniform distribution of its value.
@DachuanZhao Code above gives uniform distribution of integers in range from 0 to 2^256-1.
@Arty It's enough . Thanks very much ~~~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.