Microsoft Windows default random number generator

This document present the way the random() and srandom() function work under MS Windows 2000/XP (maybe on others too).


  • srandom() : Simply initialise a static unsigned int by the value given in parameter. Lets call this variable holdrand.

  • random() : Modify holdran as though : holdrand = holdrand * 214013L + 2531011L. Returns a "random value" computed with the new holdrand value : return (holdrand >> 16) & 0x7fff;



The following code sample implements it :

class W32Rand {

private:

unsigned int holdrand;

public:

void srand(unsigned int seed) {

holdrand = seed;

}

public:

unsigned int rand(void) {

return ((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff;

}

};