簡體   English   中英

python 中的 random.seed function 如何用於文字? 它轉換成什么數字?

[英]How does the random.seed function in python work for words? What number is it converted to?

我意識到如果我選擇

random.seed("hello world!")

它確實有效,即它不會給我任何錯誤,並且它像普通種子一樣工作; 所以例如選擇

random.seed("hello world!")
random.choices([1,2,3,4,5,6,7], k = 10)

一直給我同樣的 output; 但它實際上會很有趣,一個字符串轉換成什么數字?

random.py中的代碼如下所示:


# more code here

# Note there is a entirely different algorithm for "version 1"

elif version == 2 and isinstance(a, (str, bytes, bytearray)):
    if isinstance(a, str):
        a = a.encode()
    seed = int.from_bytes(a + _sha512(a).digest())

# some more irrelevant conditions

super().seed(seed)

所以基本上這個數字將是“輸入字符串+輸入字符串的sha512”解釋為一個非常大的integer。 Python 整數沒有最大值,所以這總是有效的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM