随机数的分布 [英] Distribution of Random Numbers

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

问题描述

我有两种代码选项:

选项1

int myFunc() {
  return new Random().nextInt();
}

或者:

选项2

private static final Random random = new Random();

int myFunc() {
  return random.nextInt();
}

我理解选项2 更惯用。我想知道选项1 的有效性。

I understand that option 2 is more idiomatic. I am wondering about the validity of option 1.

选项1 我将只使用给定种子生成的第一个数字。在选项2 中,我选择种子并使用该种子生成 n 个数字。 IIUC对此用例的随机性保证。

In option 1 I will only ever use the first number generated by a given seed. In option 2 I choose a seed and generate n numbers using that seed. IIUC the guarantees on randomness are on this use case.

因此,我的问题是,如果我打电话给选项1 很多时候对输出分布的一致性有什么保证吗?

My question is, therefore, if I call option 1 many times are there any guarantees on the uniformity of the distribution of the output?

推荐答案


我的真实问题是选项1是否在数学上有效。

My real question is whether option 1 is mathematically valid.

让我们从选项2开始。使用的随机数生成器java.util.Random 在javadoc中指定如下:

Lets start with option 2. The random number generator used by java.util.Random is specified in the javadoc as follows:


该类使用48位种子,使用线性同余公式进行修改。 (参见Donald Knuth,计算机程序设计的艺术,第2卷,第3.2.1节。)

The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 2, Section 3.2.1.)

并且有更具体的细节各种方法'javadocs。

and there is more specific detail in the various methods' javadocs.

但重点是我们使用的是由线性同余公式生成的序列,这些公式具有很大程度的自相关性。 ..这可能有问题。

But the point is that we are using a sequence generated by a linear congruential formula, and such formulae have a significant degree of auto-correlation ... which could be problematic.

现在使用选项1,您正在使用另一个 Random 实例每次种子,并应用一轮LC配方。因此,您将获得一系列可能与种子自相关的数字。但是,种子以不同的方式生成,具体取决于Java版本。

Now with option 1, you are using a different Random instance with a new seed each time, and applying one round of the LC formula. Thus you are getting a sequence of numbers that are likely to be autocorrelated with the seeds. However, the seeds are generated in different ways, depending on the Java version.

Java 6执行此操作:

Java 6 does this:

 public Random() { this(++seedUniquifier + System.nanoTime()); }
 private static volatile long seedUniquifier = 8682522807148012L;

...这根本不是随机的。如果您以恒定间隔创建 Random 实例,则种子可能间隔很近,因此您的选项#1生成的随机数序列可能是自动的-brelated。

... which is not very random at all. If you created Random instances at a constant interval, the seeds are likely to be closely spaced, and therefore the sequence of random numbers produced by your option #1 are liable to be auto-correlated.

相比之下,Java 7和8这样做:

By contrast, Java 7 and 8 do this:

 public Random() {
     this(seedUniquifier() ^ System.nanoTime());
 }

 private static long seedUniquifier() {
     // L'Ecuyer, "Tables of Linear Congruential Generators of
     // Different Sizes and Good Lattice Structure", 1999
     for (;;) {
         long current = seedUniquifier.get();
         long next = current * 181783497276652981L;
         if (seedUniquifier.compareAndSet(current, next))
             return next;
     }
 }

 private static final AtomicLong seedUniquifier
     = new AtomicLong(8682522807148012L);

上述产生的种子序列可能是(真实)随机性的更好近似值。这可能使您的选项#1优于选项#2。

The sequence of seeds produced by the above are likely be a much better approximation to (true) randomness. That probably makes your option #1 superior to option #2.

Java 6到8中选项#1的缺点是 System.nanoTime()可能调用涉及系统调用。这是相对昂贵的。

The downside of your option #1 in Java 6 through 8 is that the System.nanoTime() probably call involves a system call. That is relatively expensive.

所以简短的回答是它是特定于Java版本的选项#1和选项#从数学的角度来看,2产生质量更好的随机数字。

So the short answer is that it is Java version specific which of option #1 and option #2 produces better quality "random" numbers ... from a mathematical perspective.

在这两种情况下,数字的分布在足够大的样本量上是均匀的,尽管当流程是确定性的时候,我不确定谈论概率分布是否有意义。

In both cases, the distribution of numbers will be uniform over a large enough sample size, though I'm not sure it is meaningful to talk about probability distributions when the process is deterministic.

然而,这两种方法都不适合作为加密强度随机数生成器。

However, neither approach would be suitable as a "crypto strength" random number generator.

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

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