Python-生成相同的随机数 [英] Python - generate same random numbers

查看:702
本文介绍了Python-生成相同的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def main():
    x = [randint(1,100) for i in range(1,100)]
    return x

这将返回100和1和100之间的随机数.每次我调用该函数时,它都会返回不同的数字序列.我想要的是每次都获得相同的数字序列.也许将结果保存到某处?

This returns 100 random numbers btw 1 and 100. Each time I call the function, it returns a different sequence of numbers. What I want to, is to get the same sequence of numbers each time. maybe saving the results into sth?

推荐答案

您可以提供一些固定的种子.

You can provide some fixed seed.

import random

def main():
    random.seed(9001)
    x = [random.randint(1,100) for i in range(1,100)]
    return x

有关种子的更多信息: random.seed():它是做什么的?

For more information on seed: random.seed(): What does it do?

这篇关于Python-生成相同的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆