Java 随机种子 [英] Java Random seed

查看:41
本文介绍了Java 随机种子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对 Java 程序进行 20 次测试,并且需要设置随机种子以便可以重复测试.如果我将初始种子设置为 0,然后在每次运行时增加 1(即 1,2,3 等),即使种子相距不远,这种方法是否仍能确保完全随机性?

I need to test out a Java program 20 times and need to set the random seed so that the tests can be repeated. If I were to set the initial seed as 0 and then increment by 1 at each run (i.e. 1,2,3 etc), would this method still ensure complete randomness, even though the seeds are not far apart?

谢谢

推荐答案

对于标准 PRNG 就像 Java 中包含的那个.所以在测试中使用递增的种子是可以的.

Any seed will provide the same level of randomness as any other seed for a standard PRNG like the one included with Java. So it's fine to use an incrementing seed for tests.

不过,您可能需要考虑使用更好的随机数生成器.如果将值呈现为图像(我找不到参考,但我记得它很明显),Java 中包含的一个会产生明显的重复模式.我推荐 Mersenne Twister(有Java版本) 作为替代方案,它速度快且周期长,因此您不会轻易看到模式.

You might want to consider using a better random number generator though. The one included with Java yields noticeable repeating patterns if you render the values as an image (I can't find a reference offhand, but I recall it being apparent). I'd recommend the Mersenne Twister (there are Java versions) as an alternative that's fast and has a very long period so you won't easily see patterns.

这篇关于Java 随机种子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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