伪随机序列生成器不仅仅是一个数字生成器

发布于 2024-08-11 04:14:26 字数 316 浏览 3 评论 0原文

我需要一种算法,几乎可以将 unix 时间戳转换为适当的随机数,这样,如果我“回放”时间戳,我就会得到相同的随机数。

这就是我所说的适当的意思:

  1. 大多数人不会检测到随机数中的循环或模式。
  2. 它不必是加密安全的。
  3. 所有数字都必须能够生成。 (我发现 LFSR 不这样做)
  4. 这些数字是 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:

  1. Most humans will not detect a loop or pattern in the random numbers.
  2. It need not be cryptographically secure.
  3. All numbers must be capable of being generated. (I've found that LFSR don't do this)
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兮子 2024-08-18 04:14:26

如果不需要统计随机,可以将时间戳提供给 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.

亚希 2024-08-18 04:14:26

我建议最简单的方法是将您的时间投入到 jrand48。类似于

#include <stdlib.h>
int mix(int t) {
    unsigned short x[3] = {t, t<<16, t};
    return jrand48(x);
}

它是可逆的 (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

#include <stdlib.h>
int mix(int t) {
    unsigned short x[3] = {t, t<<16, t};
    return jrand48(x);
}

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 to x more than once too; as long as you don't apply it 248-1 times, the same sorts of properties will hold.)

寂寞花火° 2024-08-18 04:14:26

我建议查看符合 POSIX 标准的 drand48() 函数族。它们给出了不错的(但肯定不是加密的)随机数,而 srand48() 则采用 32 位种子值。它们是确定性的,因此重复使用给定的种子将再次重新生成相同的数字序列。

I suggest looking at the POSIX-compliant drand48() family of functions. They give decent (but certainly not cryptographic) random numbers, and srand48() takes a 32-bit seed value. They are deterministic, so reusing a given seed will regenerate the same sequence of numbers again.

苦行僧 2024-08-18 04:14:26

(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文