在一个只有素数的随机数生成器中播种有什么好处?

时间:2022-12-19 03:58:43

While conducting some experiments in Java, my project supervisor reminded me to seed each iteration of the experiment with a different number. He also mentioned that I should use prime numbers for the seed values. This got me thinking — why primes? Why not any other number as the seed? Also, why must the prime number be sufficiently big? Any ideas? I would've asked him this myself, but its 4am here right now, everyone's asleep, I just remembered this question and I'm burning to know the answer (I'm sure you know the feeling).

在Java中进行一些实验时,我的项目主管提醒我在每次迭代中使用不同的数字。他还提到我应该用质数来表示种子的值。这让我思考——为什么是质数?为什么没有其他数字作为种子呢?另外,为什么质数必须足够大?什么好主意吗?我本想亲自问他这个问题,但现在已经4点了,大家都在睡觉,我刚想起这个问题,我急于想知道答案(我肯定你知道那种感觉)。

It would be nice if you could provide some references, I'm very interested in the math/concept behind all this!

如果你能提供一些参考,那就太好了,我对这一切背后的数学/概念很感兴趣!

EDIT:

编辑:

I'm using java.util.Random.

我用java.util.Random。

FURTHER EDIT:

进一步的编辑:

My professor comes from a C background, but I'm using Java. Don't know if that helps. It appears that using primes is his idiosyncrasy, but I think we've unearthed some interesting answers about generating random numbers. Thanks to everyone for the effort!

我的教授来自一个C背景,但我使用的是Java。不知道这是否有用。使用素数似乎是他的癖好,但我认为我们已经找到了一些关于生成随机数的有趣答案。谢谢大家的努力!

3 个解决方案

#1


21  

Well one blink at the implementation would show you that he CAN'T have any reason for that claim at all. Why? Because that's how the set seed function looks like:

在执行过程中有一个瞬间,你会发现他根本没有任何理由。为什么?因为这就是集合种子函数的样子

synchronized public void setSeed(long seed) {
    seed = (seed ^ multiplier) & mask;
    this.seed.set(seed);
    haveNextNextGaussian = false;
}

And that's exactly what's called from the constructor. So even if you give it a prime, it won't use it anyhow, so if at all you'd have to use a seed s where (s^ multiplier) & mask results in a prime ;)

这就是构造函数的调用。因此,即使你给它一个质数,它不会不管怎样使用它,所以如果你需要使用一个种子年代(s ^乘数)&面膜'结果;)

Java uses a usual linear congruency method, i.e.:

Java使用一种通常的线性同余方法,即:

x_n+1 = (a * x_n + c) mod m with 2 <= a < m; 0 <= c < m.

x_n+1 = (a * x_n+ c)对m取2 <= a < m;0 <= c < m。

Since you want to get a maximal periode, c and m have to be relatively prime and a few other quite obscure limitations, plus a few tips how to get a practically useful version. Knuth obviously covers that in detail in part2 ;)

由于你想要得到一个最大周期,c和m必须是相对质数和一些其他相当模糊的限制,加上一些如何得到一个实际有用的版本的技巧。Knuth显然在第2部分中详细介绍了这一点)

But anyhow, the seed doesn't influence the qualities of the generator at all. Even if the implementation would be using a Lehmer generator, it would obviously make sure that N is prime (otherwise the algorithm is practically useless; and not uniformly distributed if all random values would have to be coprime to a non prime N I wager) which makes the point moot

但无论如何,种子并不影响发电机的质量。即使实现将使用Lehmer生成器,它也会明显地确保N是质数(否则算法实际上是无用的;如果所有的随机值都是与非素数N I打赌的常数,那么就不是一致分布的这就说明这一点没有意义

#2


20  

If the generator is a Lehmer generator, than the seed and the modulus must be co-prime; see the wiki page. One way to ensure they are co-prime is to start with a prime number.

如果发电机是一个Lehmer发电机,那么种子和模量必须是共撇;看到wiki页面。确保它们是连素数的一种方法是从素数开始。

#3


11  

If you are talking about java.util.Random, or one of its subclasses in the Oracle runtime, there's no reason for this. It's just a whim of your supervisor.

如果您正在谈论java.util。随机的,或者它在Oracle运行时中的一个子类,没有理由这样做。这只是你上司的心血来潮。

#1


21  

Well one blink at the implementation would show you that he CAN'T have any reason for that claim at all. Why? Because that's how the set seed function looks like:

在执行过程中有一个瞬间,你会发现他根本没有任何理由。为什么?因为这就是集合种子函数的样子

synchronized public void setSeed(long seed) {
    seed = (seed ^ multiplier) & mask;
    this.seed.set(seed);
    haveNextNextGaussian = false;
}

And that's exactly what's called from the constructor. So even if you give it a prime, it won't use it anyhow, so if at all you'd have to use a seed s where (s^ multiplier) & mask results in a prime ;)

这就是构造函数的调用。因此,即使你给它一个质数,它不会不管怎样使用它,所以如果你需要使用一个种子年代(s ^乘数)&面膜'结果;)

Java uses a usual linear congruency method, i.e.:

Java使用一种通常的线性同余方法,即:

x_n+1 = (a * x_n + c) mod m with 2 <= a < m; 0 <= c < m.

x_n+1 = (a * x_n+ c)对m取2 <= a < m;0 <= c < m。

Since you want to get a maximal periode, c and m have to be relatively prime and a few other quite obscure limitations, plus a few tips how to get a practically useful version. Knuth obviously covers that in detail in part2 ;)

由于你想要得到一个最大周期,c和m必须是相对质数和一些其他相当模糊的限制,加上一些如何得到一个实际有用的版本的技巧。Knuth显然在第2部分中详细介绍了这一点)

But anyhow, the seed doesn't influence the qualities of the generator at all. Even if the implementation would be using a Lehmer generator, it would obviously make sure that N is prime (otherwise the algorithm is practically useless; and not uniformly distributed if all random values would have to be coprime to a non prime N I wager) which makes the point moot

但无论如何,种子并不影响发电机的质量。即使实现将使用Lehmer生成器,它也会明显地确保N是质数(否则算法实际上是无用的;如果所有的随机值都是与非素数N I打赌的常数,那么就不是一致分布的这就说明这一点没有意义

#2


20  

If the generator is a Lehmer generator, than the seed and the modulus must be co-prime; see the wiki page. One way to ensure they are co-prime is to start with a prime number.

如果发电机是一个Lehmer发电机,那么种子和模量必须是共撇;看到wiki页面。确保它们是连素数的一种方法是从素数开始。

#3


11  

If you are talking about java.util.Random, or one of its subclasses in the Oracle runtime, there's no reason for this. It's just a whim of your supervisor.

如果您正在谈论java.util。随机的,或者它在Oracle运行时中的一个子类,没有理由这样做。这只是你上司的心血来潮。