当我编译一个很常见的随机数生成器mt19937ar.c时,我一直遇到需要从中获取的一些代码问题。
在函数“init_genrand”中:
警告:从'int'转换为'long unsigned int'可能会改变结果的符号。
/* Period parameters */
#define N 624
#define M 397
#define MATRIX_A 0x9908b0dfUL /* constant vector a */
#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
static unsigned long mt[N]; /* the array for the state vector */
static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
/* initializes mt[N] with a seed */
void init_genrand(unsigned long s)
{
mt[0]= s & 0xffffffffUL;
for (mti=1; mti<N; mti++) {
mt[mti] =
(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
/* In the previous versions, MSBs of the seed affect */
/* only MSBs of the array mt[]. */
/* 2002/01/09 modified by Makoto Matsumoto */
mt[mti] &= 0xffffffffUL;
/* for >32 bit machines */
}
}
具体来说,错误出在
mt[mti] =
(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
我尝试通过投射来解决它
mt[mti] = (long unsigned int)
(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
虽然这样做没有意义,但我只是尝试一下。这段代码来自1997年,我想有人应该已经发现并修复了它。我该如何解决这个问题?或者让我的编译器不再抱怨它?
30UL
是什么意思? - 8protons(mt[mti-1] >> 30
- Danh