Skip to main content
 首页 » 编程设计

python之如何从给定范围内生成在每次执行中产生相同输出的固定随机数

2024年12月31日12cmt

for x in range(16): 
    ran.append(random.randint(1, 500)) 
    # print(ran) 

在一个范围内生成随机数很容易,但是如何从给定范围内生成随机数,以便在 python 中针对该特定范围的每次执行产生相同的输出。

请您参考如下方法:

只要你seed您的 random 具有相同的种子,您将获得相同系列的值:

random.seed(7) # Or any other arbitrary seed 
 
for x in range(16): 
    ran.append(random.randint(1, 500))