Pseudocode
The following piece of pseudocode generates uniformly distributed 32-bit integers in the range with the MT19937 algorithm:
// Create a length 624 array to store the state of the generator int MT int index = 0 // Initialize the generator from a seed function initialize_generator(int seed) { MT := seed for i from 1 to 623 { // loop over each other element MT := last 32 bits of(1812433253 * (MT xor (right shift by 30 bits(MT))) + i) // 0x6c078965 } } // Extract a tempered pseudorandom number based on the index-th value, // calling generate_numbers every 624 numbers function extract_number { if index == 0 { generate_numbers } int y := MT y := y xor (right shift by 11 bits(y)) y := y xor (left shift by 7 bits(y) and (2636928640)) // 0x9d2c5680 y := y xor (left shift by 15 bits(y) and (4022730752)) // 0xefc60000 y := y xor (right shift by 18 bits(y)) index := (index + 1) mod 624 return y } // Generate an array of 624 untempered numbers function generate_numbers { for i from 0 to 623 { int y := (MT & 0x80000000) // bit 31 (32nd bit) of MT + (MT & 0x7fffffff) // bits 0-30 (first 31 bits) of MT MT := MT xor (right shift by 1 bit(y)) if (y mod 2) != 0 { // y is odd MT := MT xor (2567483615) // 0x9908b0df } } }Read more about this topic: Mersenne Twister
Related Phrases
Related Words