我将一个简单的RNG函数从C移植到Go,结果不正确
问题描述:
I am porting a simple RNG form http://en.wikipedia.org/wiki/Multiply-with-carry to Golang However, don't know which part went wrong, the result of my sample program is inconsistent.
Result:
C = 58 1 78 15 57 28 96 73 47 12 61 47 74 86 91 93
GO= 58 8 18 48 90 72 18 84 54 52 94 80 18 8 2 0
And also I don't understand why t,a use uint64 instead of uint32 in the original source.
Below is C main and Go counter part:
Go file: http://play.golang.org/p/YVyIr1bcI8
Original C:
#include <cstdlib>
#include <cstdio>
#include <stdint.h>
#define PHI 0x9e3779b9
static uint32_t Q[4096], c = 362436;
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);
}
int main( int argc, char* argv[])
{
init_rand(0);
uint32_t v=0;
for( int i=0; i<16; i++)
{
v = rand_cmwc();
printf( "%d ", (v%100));
}
char input_buf[24]={0};
printf( "
Type a character to exit:", v);
scanf("%s", input_buf);
return 0;
}
答
Huh?
The C code does 64-bit integer arithemetic when updating, keeping the upper-most 32 bits:
i = (i + 1) & 4095;
t = a * Q[i] + c;
c = (t >> 32);
In the Go code you use only 32-bit integers; of course that's not correct since it will never generate the proper upper bits.