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

Pages: 1-

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:06

It's bitmasking. The coder obviously doesn't want more than 32-bits. Why? Probably due to some analysis of the algorithm assuming 32-bits.

Why is DES limited to 56-bits? Due to the algorithm.

Note the "is a transformation which assures equidistribution of the generated numbers in 623 dimensions" in the Wikipedia. This only applies for 32-bit numbers.

Name: Anonymous 2005-07-27 8:20

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

Name: Anonymous 2005-07-27 8:48

...you must be stupid. This problem is almost made for C.

Lots of bitwise operations? Check. Need code transparency? Check. Need speed? Check. Needs to be readable to all programmers/incorporated in libraries? Check.

Name: Anonymous 2005-07-27 9:56

>>2
I know it's a mask, but I thought unsigned longs were limited to 32 bits anyway. amirong? Guess I must be.

>>3 like >>4 said.

Personally I try to avoid C for most tasks, but this is clearly a job for C. In fact the reason I'm studying it is because I want to implement it in Scheme. I know it'll be slow as hell and the code will be ugly, but what can I say, I'm a masochist.

Name: Anonymous 2005-07-27 17:01

No. Be very careful when making assumptions about the lengths of types. There are ways to ensure that a type is 32, 64, or whatever bits, but the usual types of int, long, and even char, depend on the machine architecture.

Name: Anonymous 2009-01-15 7:27

OLD SKOOL BUMP JUSTICE

Name: Christy McJesus !DcbLlAZi7U 2009-01-15 7:38

>>1
HAHA DISREGARD I SUCK COCKS

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !FrOzEn2BUo 2009-01-15 7:51

>>8
Dictionary attack?

Name: Anonymous 2009-01-15 8:59

>>9
Your virginity is showing.

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !FrOzEn2BUo 2009-01-15 9:01

>>10
He used the same tripcode as OP.

Name: Anonymous 2009-01-15 9:02

>>10
STOP SEEING HIS POSTS YOU STUPID FUCKER

Name: Anonymous 2009-01-15 9:04

>>12
Settle down, FrozenVoid's screaming companion.

Name: Anonymous 2009-01-15 9:14

>>1
Oh wow I remember this tripfag. Always thought his name was awesome.

Name: Anonymous 2009-01-15 9:17

awesome

Name: Anonymous 2009-01-15 11:02

>>14
No, his name was Christy McJesus.

Name: Anonymous 2009-01-15 16:00

There are ways to ensure that a type is 32, 64, or whatever bits
HOW?

Name: Anonymous 2009-01-15 16:01

>>17
man stdint.h

Name: Anonymous 2009-01-15 16:31

>>18
THATS c99 FAGGOT

Name: Anonymous 2009-01-15 16:45

>>19
__int32

Name: Anonymous 2009-01-15 17:23

>>17
int mt:32;

Name: Anonymous 2009-01-15 18:03

>>17
autoconf

Name: Anonymous 2009-01-15 18:45

>>21
Bit fields considered harmful

Name: Anonymous 2009-01-15 19:11

>>20-23
Same peson

Name: Anonymous 2009-01-15 20:13

>>23
Bit fields considered helpful

Name: Anonymous 2009-01-16 4:37

HAY EVERYBODY I USED A GOTO YESTERDAY. I AM SO COOL

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