Looking for a replayable / somewhat stateless PRNG algorithm
I'm looking for a pseudo-random number generator that is "replayable" and "stateless". Let me elaborate: I need to be able to re-fetch a pseudo-random number based on a parameter to the random function. For example (C-style pseudocode):
int x1 = random(1);
int x2 = random(2);
// and so on with lots of random() calls in between
int new_x1 = random(1);
// now new_x1 is like a "replay" of x1, so x1 == new_x1
The type of arguments doesn't matter (I can typecast whatever is needed), the return value doesn't have to be int
; ultimately I'll need 8-bit values.
The question is: what's a good PRNG algorithm that satisfies the requirement that the next pseudo-random value is controlled by a parameter, and not by its internal state which is updated upon each invocation? I don't what to use a crummy solution like the following:
int random(int input) {
srand(input);
return rand();
}
This would have to initialize the PRNG upon every invocation, which seems costly. (I am illustrating this point using the standard srand() / rand()
, I know there are better algorithms out there, like Mersenne Twister, but the idea is still the same.)
One approach that might work here would be to use a block cipher like AES or triple-DES. Your pseudorandom generator could then be
int pseudorandomValue(int input) {
return encryptUsingAES(input);
}
This is stateless, pseudorandom (since the outputs of AES should be statistically indistinguishable from random), and stateless.
Hope this helps!
链接地址: http://www.djcxy.com/p/6368.html
上一篇: 生成没有重复的位组合(不是permunation)
下一篇: 寻找可重放/有些无状态的PRNG算法