Re: [LINK] Faster RNG

From: George (greerga@CIRCLEMUD.ORG)
Date: 07/10/98


On Fri, 10 Jul 1998, Chris Jacobson wrote:

>Mersenne Twister is LGPL, not GPL.
>Therefore, it could be used.

Ok, that's better, Slashdot said GPL'd, not LGPL'd.  Still doesn't mean I
like it.  Go compare the two source codes:  (whitespace compressed)

unsigned long circle_random(void)
{
   register int lo, hi, test;
    hi   = seed/q;
    lo   = seed%q;
    test = a*lo - r*hi;
    if (test > 0)
        seed = test;
    else
        seed = test+ m;
    return seed;
}

And the twister:

double /* generating reals */
/* unsigned long */ /* for integer generation */
genrand()
{
    unsigned long y;
    static unsigned long mag01[2]={0x0, MATRIX_A};
    /* mag01[x] = x * MATRIX_A  for x=0,1 */
    if (mti >= N) { /* generate N words at one time */
        int kk;
        if (mti == N+1)   /* if sgenrand() has not been called, */
            sgenrand(4357); /* a default initial seed is used   */
        for (kk=0;kk<N-M;kk++) {
            y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
            mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
        }
        for (;kk<N-1;kk++) {
            y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
            mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
        }
        y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
        mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
        mti = 0;
    }
    y = mt[mti++];
    y ^= TEMPERING_SHIFT_U(y);
    y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
    y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
    y ^= TEMPERING_SHIFT_L(y);
    return ( (double)y / (unsigned long)0xffffffff ); /* reals */
    /* return y; */ /* for integer generation */
}

I think you'll see two real advantages to the current one:
1) No floating point math.
    i.e.; "return ( (double)y / (unsigned long)0xffffffff ); /* reals */"
2) Much clearer.

Hypercompacted circle random:

   register int test = a*(seed%q) - r*(seed/q);
   return (seed = (test > 0 ? test : test + m));

--
George Greer, greerga@circlemud.org | Genius may have its limitations, but
http://patches.van.ml.org/          | stupidity is not thus handicapped.
http://www.van.ml.org/CircleMUD/    |                  -- Elbert Hubbard


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST