0

因此,为了更好地理解 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^32h 是 2 个初始种子之间的差异。使用相同的逻辑,我计算了给定初始种子 asx和下一个种子 as的 2 个随机生成的数字之间的差异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。我在做一些明显错误的事情吗?

4

1 回答 1

1

新种子之间的区别确实是214013*h

这给出了种子ss + 214013*h,结果随机输出之间的差异将是(简化之前)diff = ((s + 214013*h >> 16) & 0x7fff) - ((s >> 16) & 0x7fff)。那么问题本质上是,这个表达式是否独立于s.

它不是。例如,即使取h = 1,diff也可以是 3(例如s = 0)或 4(例如s = 0x0000bc03)。

于 2017-07-07T01:21:29.397 回答