因此,为了更好地理解MSVC ++对rand
的实现,我重新实现了它,并尝试更好地理解它(我猜测一般都是LCG)。
我的实现(几乎完全匹配MSVC ++)如下:
// vc++ impl. of random
// Xn+1 = (aXn + i) mod m
// a = 214013, i = 2531011, m = 32768
unsigned int seed = 0;
unsigned int random()
{
seed = seed * 214013L + 2531011L;
// return (seed/(1<<16)) % 32768; (equiv to below)
return seed>>16 & 0x7FFF;
}
为了找到2个种子新生成的种子的差异,我认为它只是(214013*h) % 2^32
,其中h是2个初始种子之间的差异。使用相同的逻辑,我计算了2个随机生成的数字之间的差异,给定初始种子为x
,下一个种子为x+h
,我在种子中采用了这个不同,将其除以2 ^ 16(或移位)这是正确的16位),并摆脱了最重要的位。
除非在某些情况下,例如当x = 100且h = 5000时,此产生的值似乎是正确的。
以下是整个代码:
#include <iostream>
#include <cstdlib>
// vc++ impl. of random
// Xn+1 = (aXn + i) mod m
// a = 214013, i = 2531011, m = 32768
unsigned int seed = 0;
unsigned int random()
{
seed = seed * 214013L + 2531011L;
return seed>>16 & 0x7FFF;
}
int main()
{
// f(x) = (214013x + 2531011) mod 2^32 [LCG]
// g(x) = floor(f(x)/2^16) mod 2^15 [RND]
// h(x) = f(x + h) - f(x) ?= 214013*h mod 2^32
// j(x) = g(x + h) - g(x) ?= 214013*h/2^16 mod 2^15
// x: initial seed
// h: displaecment to next seed (next seed: x + h)
// a, b: first and second randomly generated values using C rand
// c, d: first and second randomly generated values using random
// newSeedA, newSeedB: seed generated from LCG after x and x + h respectively
// diffExp: experimental difference in random values
// diffCalc: calculated/theoretical difference in random vlaues
unsigned int x = 100, h = 50000;
unsigned int a, b, c, d;
unsigned int newSeedA, newSeedB;
int diffExp, diffCalc;
srand(x);
seed = x;
a = rand();
c = random();
newSeedA = seed;
srand(x + h);
seed = x + h;
b = rand();
d = random();
newSeedB = seed;
diffExp = (d - c) % 32768;
diffCalc = (214013*h)>>16 & 0x7FFF;
std::cout << "RANDOM VALUES\n";
std::cout << " VC++ rand: " << a << ", " << b << "\n";
std::cout << "Custom rand: " << c << ", " << d << "\n";
std::cout << "\n";
std::cout << "DIFFERENCE IN SEED\n";
std::cout << "Experimental Difference: " << (newSeedB - newSeedA) << "\n";
std::cout << " Calculated Difference: " << (static_cast<unsigned int>(214013)*h) << "\n";
std::cout << "\n";
std::cout << "DIFFERENCE IN VALUES\n";
std::cout << "Experimental Difference: " << diffExp << "\n";
std::cout << " Calculated Difference: " << diffCalc << "\n";
std::cout << "\n";
return 0;
}
然而,对于这些值,2个随机生成的值之间的估计差异比实际差异小1。我做了一件明显不对的事吗?
答案 0 :(得分:1)
新种子之间的区别确实是214013*h
。
这会产生种子s
和s + 214013*h
,产生的随机输出之间的差异将是(简化之前)diff = ((s + 214013*h >> 16) & 0x7fff) - ((s >> 16) & 0x7fff)
。那么问题基本上就是这个表达式独立于s
。
不是。例如,即使采用h = 1
,diff
也可以是3(例如s = 0
)或4(例如s = 0x0000bc03
)。