伪随机序列生成器不仅仅是一个数字生成器
我需要一种算法,几乎可以将 unix 时间戳转换为适当的随机数,这样,如果我“回放”时间戳,我就会得到相同的随机数。
这就是我所说的适当的意思:
- 大多数人不会检测到随机数中的循环或模式。
- 它不必是加密安全的。
- 所有数字都必须能够生成。 (我发现 LFSR 不这样做)
- 这些数字是 32 位整数
,我希望它相当快。
到目前为止,我的想法是一遍又一遍地播种 PRNG,但我不确定这是否是处理这个问题的最佳方法。
任何想法和想法将不胜感激。
谢谢。
I need an algorithm that pretty much will turn a unix timestamp into a suitably random number, so that if I "play back" the timestamps I get the same random numbers.
And here's what I mean by suitably:
- Most humans will not detect a loop or pattern in the random numbers.
- It need not be cryptographically secure.
- All numbers must be capable of being generated. (I've found that LFSR don't do this)
- The numbers are 32 bit integers
And I would like it to be fairly fast.
So far my idea is to just seed a PRNG over and over, but I'm not sure if that's the best way to handle this.
Any thoughts and ideas will be much appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果不需要统计随机,可以将时间戳提供给 MD5 并截断哈希值。主要问题是我不知道这是否是主观的。其他哈希算法可能效果更好。
If it doesn't need to be statistically random, perhaps feed the timestamps to MD5 and truncate the hash. The main issue is that I don't know if this would be surjective. Other hashing algorithms might work better.
我建议最简单的方法是将您的时间投入到 jrand48。类似于
它是可逆的 (216·x+n≡0x5deece66d·(232+1)·t+0xb mod 248 ⇒ t ≡0xdfe05bcb1365·(216·x+n-0xb) mod 248 其中 n∈[0,216)) 但因为它是48位中的高32位,其实不太容易。 (您也可以多次将
jrand48
应用于x
;只要不应用 248-1 次,就相同各种属性都将保留。)I would suggest that the easiest thing to do is feed your time to jrand48. Something like
It's reversible (216·x+n≡0x5deece66d·(232+1)·t+0xb mod 248 ⇒ t≡0xdfe05bcb1365·(216·x+n-0xb) mod 248 where n∈[0,216)) but since it's the high 32 bits out of 48-bit, it's actually not too easy. (You can apply
jrand48
tox
more than once too; as long as you don't apply it 248-1 times, the same sorts of properties will hold.)我建议查看符合 POSIX 标准的
drand48()
函数族。它们给出了不错的(但肯定不是加密的)随机数,而 srand48() 则采用 32 位种子值。它们是确定性的,因此重复使用给定的种子将再次重新生成相同的数字序列。I suggest looking at the POSIX-compliant
drand48()
family of functions. They give decent (but certainly not cryptographic) random numbers, andsrand48()
takes a 32-bit seed value. They are deterministic, so reusing a given seed will regenerate the same sequence of numbers again.(timestamp ^ 0x12345678) + 12345678
这足够微妙吗?如果您不关心它的可逆性,您可以对每个时间戳进行 crc32 处理。
(timestamp ^ 0x12345678) + 12345678
Is this subtle enough?If you don't care about reversibility of it you could crc32 each timestamp.