java.util.Random 有多好? [英] How good is java.util.Random?

查看:19
本文介绍了java.util.Random 有多好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个问题:

对于我放入的每个种子,我会得到不同的数字序列吗?

Will I get different sequences of numbers for every seed I put into it?

是否有一些死"的种子?(产生零或很快重复的那些.)

Are there some "dead" seeds? (Ones that produce zeros or repeat very quickly.)

顺便说一下,我应该使用哪些其他 PRNG(如果有)?

By the way, which, if any, other PRNGs should I use?

解决方案:由于我将使用 PRNG 制作游戏,因此我不需要它具有加密安全性.我会选择 Mersenne Twister,因为它速度快,周期长.

Solution: Since, I'm going to be using the PRNG to make a game, I don't need it to be cryptographically secure. I'm going with the Mersenne Twister, both for it's speed and huge period.

推荐答案

在某种程度上,随机数生成器是课程的马匹.Random 类使用合理选择的参数实现 LCG.但它仍然表现出以下特点:

To some extent, random number generators are horses for courses. The Random class implements an LCG with reasonably chosen parameters. But it still exhibits the following features:

  • fairly short period (2^48)
  • bits are not equally random (see my article on randomness of bit positions)
  • will only generate a small fraction of combinations of values (the famous problem of "falling in the planes")

如果这些事情对您来说并不重要,那么 Random 具有作为 JDK 的一部分提供的赎回功能.对于休闲游戏之类的东西(但不是涉及金钱的游戏)来说,这已经足够了.没有弱种子本身.

If these things don't matter to you, then Random has the redeeming feature of being provided as part of the JDK. It's good enough for things like casual games (but not ones where money is involved). There are no weak seeds as such.

另一种替代方法是 XORShift 生成器,可以用 Java 实现如下:

Another alternative which is the XORShift generator, which can be implemented in Java as follows:

public long randomLong() {
  x ^= (x << 21);
  x ^= (x >>> 35);
  x ^= (x << 4);
  return x;
}

对于一些非常便宜的操作,它的周期为 2^64-1(不允许为零),并且非常简单,可以在您重复生成值时内联.各种移位值都是可能的:有关更多详细信息,请参阅 George Marsaglia 关于 XORShift Generators 的论文.您可以将生成的数字中的位视为同样随机.一个主要的弱点是,它偶尔会陷入一个套路",即数字中没有设置多少位,然后需要几代人才能摆脱这种套路.

For some very cheap operations, this has a period of 2^64-1 (zero is not permitted), and is simple enough to be inlined when you're generating values repeatedly. Various shift values are possible: see George Marsaglia's paper on XORShift Generators for more details. You can consider bits in the numbers generated as being equally random. One main weakness is that occasionally it will get into a "rut" where not many bits are set in the number, and then it takes a few generations to get out of this rut.

其他可能性是:

  • 组合不同的生成器(例如,将 XORShift 生成器的输出馈入 LCG,然后将结果添加到具有不同参数的 XORShift 生成器的输出中):这通常可以消除"不同方法的弱点, 如果仔细选择组合发电机的周期,可以给出更长的周期
  • 添加一个滞后"(以提供更长的时间):本质上,生成器通常会转换生成的最后一个数字,存储历史缓冲区"并转换,例如,第 (n-1023) 个.

我会说避免使用愚蠢的内存量来给你一个比你真正需要的周期更长的生成器(有些周期大于宇宙中的原子数量——你通常不需要那个).请注意,长周期"并不一定意味着高质量的生成器"(尽管 2^48 仍然有点低!).

I would say avoid generators that use a stupid amount of memory to give you a period longer than you really need (some have a period greater than the number of atoms in the universe-- you really don't usually need that). And note that "long period" doesn't necessarily mean "high quality generator" (though 2^48 is still a little bit low!).

这篇关于java.util.Random 有多好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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