-1

http://en.wikipedia.org/wiki/Multiply-with-carry#Complementary-multiply-with-carry_RNGs 有一些 C 代码

我正在尝试编译它,但是当我这样做时,我收到此错误 c:\program files (x86)\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1......\libmingw32 .a(main.o):main.c|| 未定义对“WinMain@16”的引用|

这是代码

我希望弄清楚如何实现它并使用它来代替 booster 的 mersenne twister。

正如有人指出的那样,我错过了我的主要功能,但现在有了它,我仍然无法让它运行。

#include <stdint.h>

#define PHI 0x9e3779b9

static uint32_t Q[4096], c = 362436;



int main
{
    init_rand(6);

    int c = rand_cmwc();
};

void init_rand(uint32_t x)
{
        int i;

        Q[0] = x;
        Q[1] = x + PHI;
        Q[2] = x + PHI + PHI;

        for (i = 3; i < 4096; i++)
                Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
}

uint32_t rand_cmwc(void)
{
        uint64_t t, a = 18782LL;
        static uint32_t i = 4095;
        uint32_t x, r = 0xfffffffe;
        i = (i + 1) & 4095;
        t = a * Q[i] + c;
        c = (t >> 32);
        x = t + c;
        if (x < c) {
                x++;
                c++;
        }
        return (Q[i] = r - x);
}
4

1 回答 1

3

好吧,我想通了。

我错过了我的主要功能。

然后我将代码实现为一个类,希望使其线程安全(允许多个种子)

#include <iostream>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#define PHI 0x9e3779b9

using namespace std;

static uint32_t Q[4096], c = 362436;

class cmwc
{

  public:

    void init_rand(uint32_t x)
    {
        int i;

        Q[0] = x;
        Q[1] = x + PHI;
        Q[2] = x + PHI + PHI;

        for (i = 3; i < 4096; i++)
                Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
    }

    uint32_t rand_cmwc(void)
    {
        uint64_t t, a = 18782LL;
        static uint32_t i = 4095;
        uint32_t x, r = 0xfffffffe;
        i = (i + 1) & 4095;
        t = a * Q[i] + c;
        c = (t >> 32);
        x = t + c;
        if (x < c) {
                x++;
                c++;
        }
        return (Q[i] = r - x);
    }

    uint32_t random_num;

};

int main()
{
    cmwc rng1;

    rng1.init_rand(time(NULL));

    rng1.random_num = 1 + rng1.rand_cmwc()%6;

    //int r = rng1.rand_cmwc();

    cout << "die: " << rng1.random_num << endl;


    return 0;
}
于 2012-10-14T22:39:04.547 回答