基于散列的播种已被弃用

Seeding based on hashing is deprecated

下面的函数收集随机数,但 seed 参数应该是可选的。因此,我添加了 * .

import random

def random_num(n, number, *seed): #n is the number of values to return and number is the range of the uniform distribution that uses later
    collect = []
    random.seed(seed)
    for i in range(n):
        collect.append(round(random.uniform(-number, number),2))    
    return collect

运行 不使用 seed 参数的函数:

random_num(4,5)

结果是:[4.82, -3.12, -0.62, 0.27] 我猜这看起来不错。它还显示以下警告:

DeprecationWarning: Seeding based on hashing is deprecated since Python 3.9 and will be removed in a subsequent version. The only supported seed types are: None, int, float, str, bytes, and bytearray. random.seed(seed)

简单地使种子参数可选而没有问题的正确方法是什么?

您可以将种子默认为 'None'

def random_num(n, number, seed=None): 
    random.seed(seed)
    [...]

然后使用默认种子(当前系统时间) https://docs.python.org/3/library/random.html