代码在 Objective C 中,但是即使你不了解 Objective C,如果你仔细查看它应该是可以理解的。基本上它是一个 RNG 对象,你实例化一个新实例,如果你想要设置种子并开始抓取随机数。
那么是否可以回溯给定的一系列数字以确定用于生成数字的种子?我猜任何给定的算法都不能生成任何随机数字集,或者可以吗?
假设我执行以下操作:
rng.seed = 1024;
for (int i=1; i<11; i++)
DLog(@"%lu", [rng randomBetween:0 and:10]);
这给了我序列10, 10, 8, 10, 2, 10, 9, 9, 7, 4
。给定序列,我可以使用某种方法或算法来获得数字 1024?我知道那是看到的 1024 的有效序列,但我只是组成一个序列10, 1, 9, 6, 3, 9, 10, 3, 5, 2
...... 有没有办法知道这是否是该算法的有效序列,如果是,种子是什么?
RNG.h:
@interface RNG : NSObject
@property (assign) unsigned long seed;
- (unsigned long)random;
- (long)randomBetween: (long)min and: (long)max;
@end
RNG.m:
#define A 16807 /* a relatively prime number -- also M div Q */
#define M 2147483647L /* 0xFFFFFFFF / 2 */
#define Q 127773L /* M div A */
#define R 2836 /* M mod A */
@implementation RNG
@synthesize seed = _seed;
- (id)init {
self = [super init];
if (self) {
self.seed = 0;
}
return self;
}
- (unsigned long)random {
self.seed = A * (self.seed % Q) - R * (self.seed / Q);
if (self.seed > M)
return (self.seed -= M);
else if (self.seed)
return (self.seed);
else
return (self.seed = 1L);
}
- (long)randomBetween: (long)min and: (long)max {
return ([self random] % (max - min + 1) + min);
}
- (void)seed: (unsigned long)new_seed {
if (new_seed == 0)
new_seed = 1;
while (new_seed > M)
new_seed -= M;
self.seed = new_seed;
}
@end