如何查询random.random()使用的种子? [英] how to query seed used by random.random()?

查看:286
本文介绍了如何查询random.random()使用的种子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法找出Python用来为其随机数生成器提供种子的种子?

我知道我可以指定自己的种子,但是我对Python对其进行管理感到非常满意.但是,我确实想知道它使用了什么种子,以便如果我喜欢在特定运行中获得的结果,则可以稍后重现该运行.如果我拥有使用过的种子,那我可以.

如果答案是我不能,那么自己生成种子的最佳方法是什么?我希望它们在每次运行中都始终与众不同-我只想知道使用了什么.

更新:是的,我的意思是random.random()!错误... [标题已更新]

解决方案

不可能从生成器中获取自动种子.我通常会生成这样的种子:

seed = random.randrange(sys.maxsize)
rng = random.Random(seed)
print("Seed was:", seed)

这种方式是基于时间的,因此每次(手动)运行脚本时都会有所不同,但是如果您使用多个生成器,它们将不会具有相同的种子,因为它们几乎是同时创建的./p>

Is there any way to find out what seed Python used to seed its random number generator?

I know I can specify my own seed, but I'm quite happy with Python managing it. But, I do want to know what seed it used, so that if I like the results I'm getting in a particular run, I could reproduce that run later. If I had the seed that was used then I could.

If the answer is I can't, then what's the best way to generate a seed myself? I want them to always be different from run to run---I just want to know what was used.

UPDATE: yes, I mean random.random()! mistake... [title updated]

解决方案

It is not possible to get the automatic seed back out from the generator. I normally generate seeds like this:

seed = random.randrange(sys.maxsize)
rng = random.Random(seed)
print("Seed was:", seed)

This way it is time-based, so each time you run the script (manually) it will be different, but if you are using multiple generators they won't have the same seed simply because they were created almost simultaneously.

这篇关于如何查询random.random()使用的种子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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