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

查看:92
本文介绍了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:

  • 很短的时间(2 ^ 48)
  • 位的随机性不同(请参见我在位位置随机性上的文章)
  • 只会生成值的 combinations 的一小部分("掉进飞机)
  • 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上的论文.您可以将生成的数字中的位视为同等随机.一个主要的缺点是,有时它会进入"rut",而在数字中设置的位数并不多,然后需要几代人的时间才能摆脱该rut.

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天全站免登陆