Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C help

Name: Christy McJesus !DcbLlAZi7U 2005-07-27 7:54

Hello. I've recently taken a minor interest in random number generators, and have been studying the Mersenne Twister algorithm. (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html)

The first of the seeding functions uses an unsigned long to seed the array, but it does a curious thing. Here is the code:

    void init_genrand(unsigned long s)
    {
        mt[0]= s & 0xffffffffUL;

As far as I can tell this is an identity operation: i.e. for all values of s, s & 0xffffffff == s

In other words it does nothing.

Later on in the function he does it again, but this time with a comment that appears to explain it:

    mt[mti] &= 0xffffffffUL;
    /* for >32 bit machines */

Can anyone suggest why the author put this in his code? I can't see how a 64 bit machine would affect the calculation at all, but I don't know much C which probably explains my ignorance.

Here's the whole function along with the (ew) global variables it uses:

    #define N 624
    static unsigned long mt[N]; /* the array for the state vector  */
    static int mti = N+1; /* mti == N+1 means mt is not initialized */

    /* initializes mt 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.
             * 002/01/09 modified by Makoto Matsumoto */
             mt[mti] &= 0xffffffffUL;
             /* for >32 bit machines */
        }
    }

Name: Anonymous 2005-07-27 8:20

Why in the world would you want to do this in C?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List