使用时间作为随机数生成的种子还有什么问题?
我知道时间是随机数生成的不安全种子,因为它实际上 减小种子空间的大小。
但说我不关心安全。例如,假设我正在为纸牌游戏进行蒙特卡罗模拟。然而,我确实关心尽可能接近真正的随机性。作为种子的时间会影响我输出的随机性吗?我认为在这种情况下 PRNG 的选择比种子更重要。
I understand that time is an insecure seed for random number generation because it effectively reduces the size of the seed space.
But say I don't care about security. For example, say I'm doing a Monte Carlo simulation for a card game. I DO however, care about getting as close to true randomness as possible. Will time as a seed affect the randomness of my output? I would think the choice of PRNG matters more than the seed in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
出于安全目的,您显然需要高熵种子。而光靠时间是无法做到这一点的。
出于模拟目的,种子的质量并不重要,只要它是唯一的即可。正如您所指出的,PRNG 的质量在这里更为重要。
即使是游戏中的 PRNG 也可能需要安全。例如,在多人游戏中,玩家可能会找出 PRNG 的内部状态,并用它来预测未来的随机事件、猜测对手的牌、获得更好的战利品……
使用时间来播种 PRNG 的一个常见陷阱是时间不经常改变。例如,在 Windows 上,大多数与时间相关的函数仅每隔几毫秒更改其返回值。因此,在该时间间隔内创建的所有 PRNG 将返回相同的序列。
For security purposes you obviously need a high entropy seed. And time alone cannot provide that.
For simulation purposes the quality of the seed doesn't matter much, as long as it's unique. As you noted the quality of the PRNG is more important here.
Even a PRNG in a game may need to be secure. For example in multiplayer games a player might find out the internal state of the PRNG and use that to predict future random events, guess the opponent cards, get better loot,...
One common pitfall using time to seed a PRNG is that the time doesn't change very often. For example on windows most time related functions only change their return value every few milliseconds. So all PRNGs created withing that interval will return the same sequence.
为了完整起见,Matsumoto 等人的这篇论文。很好地说明了初始化方案(即选择种子的方式)对于模拟的重要性。事实证明,即使 RNG 算法本身在原理上相当好,但糟糕的初始化方案可能会严重偏差结果。
Just for the sake of completeness, this paper by Matsumoto et al. nicely illustrates how important the initialization scheme (ie. the way of choosing your seed(s)) is for simulation. Turns out a bad initialization scheme may strongly bias the results, even though the RNG algorithm as such is rather good in principle.
如果您只是运行程序的单个实例,那么应该不会有太多问题。
然而,我见过有人同时启动多个程序,然后每个程序随着时间的推移而播种。在这种情况下,所有程序都会获得相同的随机数字序列——特别是我看到人们在每次调用时播种一个apache进程,以使用随机数字作为会话- id,却发现不同的人同时访问网络服务器会得到完全相同的ID。
因此,如果您希望同时运行该程序的多个版本,那么使用时间是一个非常糟糕的主意。
If you are just running a single instance of your program, then there should not be too many problems.
However I have seen people who starts multiple programs at the same time and then each program seed with time. In that case all the program gets the same sequence of random numbers -- In particular I have seen people seeding an apache process at each call to use a random numer as session-id, only to find that different people hitting the webserver at the same time get exactly the same IDs.
Hence if you are expecting to run multiple simultanous version of the program, then using time is a very bad idea.
认为您的程序运行得非常快,并且要求系统时间以很大的顺序用作种子,间隔非常短。您可以获得与答案相同的时间,因此最终会生成相同的随机数。因此,即使在模拟中,低熵也可能是一个问题。
考虑到在您的系统中拥有一些其他熵源并不难,甚至您的操作系统也可以为您提供一些几乎随机的数字,您可以使用它们来增加基于时间的种子的熵。
Think that your program runs very fast and asks for the system's time to use as a seed in a great sequence, with a very few interval. You could get the same time as the answer, so it would end up generating the same random number. So, even in a simulation, a low-entropy can be a problem.
Considering that it's not that hard to have some other sources of entropy in your system, ot that even your operating system can provide you some almost-random numbers, you could use them to increase the entropy of your time-based-seed.