Paste: rng.clay

Author: pruned
Mode: javascript
Date: Thu, 14 Apr 2011 13:52:05
Plain Text |
/*
 unsigned long KISS(){ 
  static unsigned long x=123456789,y=362436000,z=521288629,c=7654321; 
  unsigned long long t, a=698769069LL; 
  x=69069*x+12345; 
  y^=(y<<13); y^=(y>>17); y^=(y<<5); 
  t=a*z+c; c=(t>>32); 
  return x+y+(z=t); 
                    } 
*/

record Kiss
(
    x: UInt32,
    y: UInt32,
    z: UInt32,
    c: UInt32,
);

overload Kiss() = Kiss(123456789u32,362436000u32,521288629u32,7654321u32);

procedure random32;

overload random32(this: Kiss) UInt32
{
    ref x = this.x; ref y = this.y; ref z = this.z; ref c = this.c;
    var a = 698769069u64;
    x = 69096*x+12345;
    y = bitwiseXor(y,shiftLeft(y,13));
    y = bitwiseXor(y,shiftRight(y,17));
    y = bitwiseXor(y,shiftLeft(y,5));
    var t = a*z+c;
    c = UInt32(shiftRight(t, 32));
    z = UInt32(t);
    return x+y+z;
}

record CMWC4k
(
    Q: Array[UInt32, 4096],
    c: UInt32,
    i: Int32,
);

overload CMWC4k(rng) r: CMWC4k
{
    for (x in r.Q)
        x = random32(rng);
        
    r.c = 362436;
    r.i = 4095;
}

CMWC4k_kiss() r: CMWC4k
{
    var rng = Kiss();
    for (x in r.Q)
        //x = random32(rng)*1024*64+random32(rng); // bad mixing otherwise...
        x = random32(rng)+random32(rng); // bad mixing otherwise...
        
    r.c = 362436;
    r.i = 4095;
}

overload CMWC4k() = CMWC4k(CMWC4k_kiss());
//overload CMWC4k() = CMWC4k_kiss();

overload random32(this: CMWC4k) UInt32
{
    ref i = this.i; ref c = this.c; ref Q = this.Q;

    var a = 18782u64;
    var b = 4294967295u64;
    
    var r = b-1;
    i = bitwiseAnd(i+1, 4095);
    var t = a*Q[i]+c;
    c = UInt32(shiftRight(t, 32));
    t = bitwiseAnd(t, b)+c;
    if (t>r) 
    {
        c += 1;
        t = t-b;
    }
    Q[i] = UInt32(r-t);
    return Q[i];
}

record MT
(
    state: Array[UInt32, 624],
    i: Int,
);

overload MT(seed) r: MT
{
    r.state[0] = seed;
    r.i = 0;
    for (x in range(1,624))
    {
        var v = 1812433253u64 * bitwiseXor(r.state[x-1], shiftRight(r.state[x-1], 30)) + 1;
        r.state[x] = UInt32(v);
    }
}

overload random32(this: MT)
{
    ref i = this.i; ref state = this.state;
    if (i == 0)
        generateNumbers(this);
    
    var y = state[i];
    y = bitwiseXor(y, shiftRight(y, 11));
    y = bitwiseXor(y, bitwiseAnd(shiftLeft(y, 7), 2636928640u32));
    y = bitwiseXor(y, bitwiseAnd(shiftLeft(y, 15), 4022730752u32));
    y = bitwiseXor(y, shiftRight(y, 18));
    
    i = (i + 1) % 624;
    return y;
}

generateNumbers(this: MT)
{
    ref state = this.state;
    
    for (i in range(624))
    {
        var y = shiftRight(state[i], 31) + bitwiseAnd(state[(i+1)%624], 0x7FFFFFFFu32);
        state[i] = bitwiseXor(state[(i+397) % 624], shiftRight(y, 1));
        if (bitwiseAnd(y,1) == 1)
        {
            state[i] = bitwiseXor(state[i], 2567483615u32);
        }
    }
}

record DummyRNG
(
    c: UInt32,
);

overload DummyRNG(x) r: DummyRNG
{
    r.c = UInt32(x);
}

overload random32(this: DummyRNG)
{
    this.c += 1;
    return this.c; 
}




New Annotation

Summary:
Author:
Mode:
Body: