Python中的Seed()和随机数 [英] Seed() and Random Numbers in Python

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

问题描述

可能重复:
Python的随机性:如果我不使用seed(someValue )?

Possible Duplicate:
Python's random: What happens if I don't use seed(someValue)?

今天,我刚从比我有经验的程序员那里得知了seed()函数的信息.通常,我只用列表作为参数调用choice(),因为我不再需要随机数功能.

Today, I was just told about the seed() function from a programmer much more experienced than me. I normally just call choice() with a list as an argument, as I don't need anymore random number functionality than that.

我的程序员朋友告诉我,调用种子是必要的,因为否则Python总是以零作为默认种子开始随机数运算.这意味着尽管数字看起来是随机的,但实际上每次我们得到的序列都是相同的.

My programmer friend told me that calling seed is necessary because otherwise Python always begins random number operations with zero as the default seed. This means that although the numbers appear random, we are really getting the same sequence every time.

这让我很奇怪.例如,choice()函数是否真的没有在执行其工作之前调用种子?或者是它之所以不能以编程方式更改其种子的原因,因为它本身会涉及到选择一个随机数,并且显然,如果我们的最终目标也是选择一个随机数,那将是一个问题!

This strikes me as rather odd. Does the choice() function, for example, really not call seed before it does its thing? Or is the reason it can't programmatically change its seed because that in of itself would involve picking a random number, and obviously that it is a bit of a problem if our end goal is also to pick a random number!

我在这里有点夸张,但我想知道是否有人清楚如何实现所有这些.

I'm ranting a bit here, but I'm wondering if anyone has a clear idea of how all this was implemented.

推荐答案

您的朋友是完全错误的,如果他阅读了seed()函数的文档,会知道的:

Your friend is dead wrong, and would know so if he read the documentation for the seed() function:

初始化基本随机数生成器.可选参数x可以是任何可哈希对象.如果省略x或无",则使用当前系统时间; 在首次导入模块时,还使用当前系统时间来初始化生成器.如果操作系统提供了随机性源,则将使用它们代替系统时间(请参见os.urandom()功能以获取有关可用性的详细信息).

Initialize the basic random number generator. Optional argument x can be any hashable object. If x is omitted or None, current system time is used; current system time is also used to initialize the generator when the module is first imported. If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom() function for details on availability).

(强调我的.)

他是基于他对其他语言的工作原理的猜测.主要提供seed()函数,以便您可以获取可复制的伪随机数流(某些特定应用程序必需).

He's guessing based on his knowledge of how it works in other languages. The seed() function is mainly provided so that you can get a reproducible stream of pseudorandom numbers (which is necessary for some specific applications).

直接从random模块调用的函数实际上是random.Random类的隐藏实例的方法的别名.每个实例至少有效地在其__init__()中调用seed().

The functions you call directly from the random module are actually aliases to methods of a hidden instance of the random.Random class. Each instance, at least in effect, calls seed() within its __init__().

choice()函数显然不会在操作前调用seed(),因为这意味着在每次选择之前都要重新播种,这不利于播种的目的.

The choice() function obviously does not call seed() before operation, because that would mean re-seeding before every choice, which defeats the purpose of seeding.

这篇关于Python中的Seed()和随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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