// Random48.h - CSC 601 - Fall 2002 - Kirby // // A common 48-bit random number generator. // See http://www.opengroup.org/onlinepubs/007908799/xsh/drand48.html. // // Example usage: // // Random48 rand( 1960 ) ; // int i= rand.next() % n ; // random int 0...n-1 // #include #include class Random48 { public: Random48( unsigned int seed ) : _hi( seed ), _lo( 0x330E ) {} int next() { _hi = (_hi * 0xDEECE66D) + (_lo * 0x5DEEC) ; _lo = _lo * 0xE66D + 0xB ; _hi = _hi + (_lo >> 16) ; _lo = _lo & 0xFFFF ; return (_hi >> 1) ; } private: unsigned int _hi, _lo ; } ;