//ShortRand.h #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000 #ifndef ShortRandh #define ShortRandh /* A Pseudo Random Number Generator based on Knuth, Vol 2, p. 27. Returns shorts uniformly distributed over -32768 to 32767 The numbers always occur in the same sequence (invariable seed). #include "ShortRand.h" main() { CShortRand SRand; for(;;) { short i=++SRand; WORD d=++SRand; } } */ class CShortRand { short i; char j,k; public: CShortRand() : j(1), k(4) {} operator short() {return i;} short operator++() {return Rand();} private: short Rand() { static short y[]={-21161, -8478, 30892,-10216, 16950}; y[k]+=y[j]; //16 bit 2's complement addition with overflow checking disabled if(y[k] > 32767) y[k] = -(32768 - (y[k] & 32767)); //make 16 bit rollover to -/+ if(y[k] < -32768) y[k] = y[k] & 32767; i=y[k]; if(--k<0) k=4; if(--j<0) j=4; return i; } }; #endif //ndef ShortRandh