MKoD - D Programming Language

D-Sourcery, getting random numbers using kiss in D - code-name RandomKiss.d

Very Kool! Random Kiss example:

/+
 ' Source   : RandomKiss.d - random number functions
 ' Authors  : Kris shares the D version of the Kiss Random Number function,
 '          : which he uses in his Mango library.
 '          : KISS (via George Marsaglia and Paul Hsieh)
 ' Created  : 11.Nov.05 (Posted on the D forum)
 ' Modified : 26.Jul.06 David L. Davis - created a kiss-able example, with a 
 '          : getRandomKissRanged() function that can use a date for repeating
 '          : random numbers. 
 ' Ref      : http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=29991
 ' ----------------------------------------------------------------------------
 ' To run an example: dmd randomkiss.d -debug=randomkiss
 ' To compile       : dmd yourcode.d randomkiss.d
 +/
module randomkiss;
private import std.date;
private import std.stdio;

static final uint getRandomKiss()
{
    static uint kiss_x = 1;
    static uint kiss_y = 2;
    static uint kiss_z = 4;
    static uint kiss_w = 8;
    static uint kiss_carry = 0;
    static uint kiss_k;
    static uint kiss_m;
    
    kiss_x = kiss_x * 69069 + 1;
    kiss_y ^= kiss_y << 13;
    kiss_y ^= kiss_y >> 17;
    kiss_y ^= kiss_y << 5;
    kiss_k = (kiss_z >> 2) + (kiss_w >> 3) + (kiss_carry >> 2);
    kiss_m = kiss_w + kiss_w + kiss_z + kiss_carry;
    kiss_z = kiss_w;
    kiss_w = kiss_m;
    kiss_carry = kiss_k >> 30;
    
    return (kiss_x + kiss_y + kiss_w);
}

final uint getRandomKissRange(in uint uiLowNum, in uint uiHighNum, in bool bMixTime = false)
{
    if (uiHighNum <= 0)
        return uiLowNum;
        
    if (uiLowNum == uiHighNum || uiLowNum > uiHighNum)
        return uiLowNum;

    uint   kiss_me = getRandomKiss();	
    d_time sysTime; 
    
    //writefln("uiHighNum=%d, uiLowNum=%d, other=%d", uiHighNum, uiLowNum, uiHighNum);
    //writefln("kiss=%d, other=%d", kiss_me, (uiHighNum - uiLowNum + 1) + uiLowNum);

    if (!bMixTime )
    {
        if (uiLowNum == 0x0 && uiHighNum == 0xFFFFFFFF)
            // Normal get()
            return kiss_me;
        else
            // Ranged get(x,y)
            return ((kiss_me % (uiHighNum - uiLowNum + 1)) + uiLowNum);
    }
    else
    {    
        sysTime = (msFromTime(getUTCtime() + 2)); 
    
        if (uiLowNum == 0x0 && uiHighNum == 0xFFFFFFFF)
            // Normal get()
            return kiss_me % sysTime;
        else
            // Ranged get(x,y)
            return (((kiss_me % sysTime) % (uiHighNum - uiLowNum + 1)) + uiLowNum);
    }        
}

debug( randomkiss )
{
int main()
{
    enum : ubyte { Red=0, Blue=1, Green=2, Yellow=3, Orange=4 }
    
    uint[ 5 ] ubytCtr = 0U; 
    ubyte     ubytRN  = 0U;
    int       ix      = 0;
    
    // Select how many pieces of candy the bag has.
    int iNumOfKisses = getRandomKissRange( 41, 57, true ); 
    
    writefln( "Your Hershey's Kiss-ables candy coated mini kisses are being made...\n" );
    
    for( ;; )
    {
        // Select a candy's color.
        ubytRN = getRandomKissRange( 0, 4, true ); 
        
        switch ( ubytRN )
        {
           case Red:    { if ( ubytCtr[ Red    ] < 15 ) { writef( "Red,    " ); ubytCtr[ Red    ]++; } else continue; break; }
           case Blue:   { if ( ubytCtr[ Blue   ] < 15 ) { writef( "Blue,   " ); ubytCtr[ Blue   ]++; } else continue; break; }
           case Green:  { if ( ubytCtr[ Green  ] < 15 ) { writef( "Green,  " ); ubytCtr[ Green  ]++; } else continue; break; }
           case Yellow: { if ( ubytCtr[ Yellow ] < 15 ) { writef( "Yellow, " ); ubytCtr[ Yellow ]++; } else continue; break; }
           case Orange: { if ( ubytCtr[ Orange ] < 15 ) { writef( "Orange, " ); ubytCtr[ Orange ]++; } else continue; break; }
           default : continue;
        }
        
        ix++;
        if ( ix % 10 == 0 ) writefln();
        if ( ix == iNumOfKisses ) break;
    }
    
    writefln( "\n" );
    writefln( "Your bag of %s Hershey's Kiss-ables has the following amount in each color:", iNumOfKisses );
    writefln( "Red=%2d, Blue   =%2d, Green  =%2d, Yellow =%2d, Orange=%2d",  
               ubytCtr[ Red ], ubytCtr[ Blue ], ubytCtr[ Green ], ubytCtr[ Yellow ], ubytCtr[ Orange ] );
         
    return 0;    
}
}
C:\dmd>dmd randomkiss.d -debug=randomkiss
C:\dmd\bin\..\..\dm\bin\link.exe randomkiss,,,user32+kernel32/noi;

C:\dmd>randomkiss
Your Hershey's Kiss-ables candy coated mini kisses are being made...

Orange, Orange, Blue,   Yellow, Blue,   Blue,   Yellow, Blue,   Yellow, Green,
Red,    Yellow, Yellow, Green,  Orange, Green,  Orange, Green,  Green,  Yellow,
Orange, Red,    Orange, Red,    Orange, Orange, Orange, Red,    Green,  Yellow,
Green,  Blue,   Orange, Red,    Red,    Blue,   Red,    Yellow, Orange, Green,
Green,  Orange, Green,  Green,  Red,    Blue,   Yellow, Green,  Yellow, Yellow,
Green,

Your bag of 51 Hershey's Kiss-ables has the following amount in each color:
Red= 8, Blue   = 7, Green  =13, Yellow =11, Orange=12

C:\dmd>randomkiss
Your Hershey's Kiss-ables candy coated mini kisses are being made...

Green,  Red,    Red,    Green,  Orange, Green,  Yellow, Red,    Green,  Blue,
Green,  Red,    Orange, Yellow, Blue,   Green,  Yellow, Blue,   Yellow, Orange,
Blue,   Orange, Red,    Green,  Blue,   Red,    Red,    Red,    Yellow, Green,
Yellow, Red,    Orange, Yellow, Orange, Yellow, Blue,   Red,    Yellow, Green,
Green,  Red,    Red,    Blue,   Green,

Your bag of 45 Hershey's Kiss-ables has the following amount in each color:
Red=12, Blue   = 7, Green  =11, Yellow = 9, Orange= 6

C:\dmd>randomkiss
Your Hershey's Kiss-ables candy coated mini kisses are being made...

Red,    Green,  Blue,   Red,    Orange, Yellow, Blue,   Blue,   Orange, Yellow,
Green,  Orange, Yellow, Green,  Blue,   Orange, Red,    Orange, Orange, Orange,
Orange, Orange, Red,    Green,  Green,  Yellow, Green,  Yellow, Green,  Orange,
Yellow, Red,    Red,    Blue,   Blue,   Red,    Yellow, Yellow, Blue,   Blue,
Yellow, Red,    Red,    Red,    Green,  Orange, Orange, Red,    Orange, Blue,


Your bag of 50 Hershey's Kiss-ables has the following amount in each color:
Red=11, Blue   = 9, Green  = 8, Yellow = 9, Orange=13

C:\dmd>
Mars: fourth Rock from the Sun.