在 Python 中使用 random 时是否需要调用 seed() ? [英] Is it necessary to call seed() when using random in Python?

查看:37
本文介绍了在 Python 中使用 random 时是否需要调用 seed() ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看以下代码:

from random import choice

for val in range(10):
    a = ','.join(str(choice(range(20))) for idx in range(4))
    print a

然后意识到我没有使用过seed().如果您打算生成不同的伪随机序列,我已经学会了为随机数生成器设置种子.

And realized that I hadn't used seed(). I've been taught to seed the random number generator if you intend to generate different psuedo-random sequences.

我决定运行代码,期望每次都重复这个序列.但是经过几次代码运行后,它似乎每次都会生成不同的序列.

I decided to run the code, expecting to the sequence repeated each time. But after several runs of the code, it appears to generate a different sequence each time.

  1. 真的有必要为 Python 随机数生成器设置种子吗?或者...
  2. 是否在某处默认调用了种子?或者...
  3. 我是不是做错了什么和/或不明白发生了什么?

推荐答案

我认为种子只是为了使用

I think seed is just meant to be used

1) 这样如果你用相同的数字播种,你每次都能得到相同的可预测序列

1) so that you can get the same predictable sequence every time if you seed with the same number

2) 提供更好的(例如硬件生成的)随机数作为起始值

2) to feed a better (eg hardware generated) random number in as a start value

这篇关于在 Python 中使用 random 时是否需要调用 seed() ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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