仅使用素数作为随机数生成器的种子有什么好处?
在用 Java 进行一些实验时,我的项目主管提醒我在实验的每次迭代中使用不同的数字作为种子。他还提到我应该使用素数作为种子值。这让我思考——为什么是素数?为什么不使用其他数字作为种子呢?另外,为什么素数必须足够大?有什么想法吗?我本来会亲自问他这个问题,但现在是凌晨 4 点,每个人都在睡觉,我刚刚想起这个问题,我很想知道答案(我相信你知道这种感觉)。
如果您能提供一些参考资料那就太好了,我对这一切背后的数学/概念非常感兴趣!
编辑:
我正在使用java.util.Random。
进一步编辑:
我的教授有 C 背景,但我使用的是 Java。不知道这是否有帮助。看来使用素数是他的特质,但我认为我们已经发现了一些关于生成随机数的有趣答案。感谢大家的努力!
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).
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.
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,只要一眨眼的实施,你就会发现他根本没有任何理由提出这种说法。为什么?因为这就是设置种子函数的样子:
这正是构造函数所调用的。因此,即使你给它一个素数,它也不会使用它,所以如果有的话,你必须使用种子 s where (s^ multiplier) & mask 结果是素数;)
Java 使用通常的线性同余方法,即:
x_n+1 = (a * x_n + c) mod m 其中 2 <= a <米; 0 <= c <米。
由于您想要获得最大周期,因此 c 和 m 必须互质,还有一些其他相当模糊的限制,以及如何获得实际有用的版本的一些技巧。 Knuth 显然在第 2 部分中详细介绍了这一点;)
但无论如何,种子根本不会影响生成器的质量。即使实现使用 Lehmer 生成器,它显然也会确保 N 是素数(否则该算法实际上是无用的;并且如果所有随机值必须与非素数 NI 赌注互质,则不是均匀分布的),这使得毫无意义的观点
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:
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 ;)
Java uses a usual linear congruency method, i.e.:
x_n+1 = (a * x_n + c) mod m with 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 ;)
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 生成器,则种子和模数必须互质;请参阅维基页面。确保它们互质的一种方法是从质数开始。
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.
如果您正在谈论
java.util.Random
或其在 Oracle 运行时中的子类之一,则没有理由这样做。这只是你的主管的一时兴起。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.