Rand- 16 bits only????

Miscellaneous Forums/Blitz Showcase/Rand- 16 bits only????

From
http://www.agner.org/random/

Note: this is a typical pseudo-random number generator (note that most random number generators are pseudo; the only true random number generators rely on some allegedly truly random input, such as observations of quantum mechanical behavior).

Mother-of-All
pseudo random number generator

This is a so-called multiply-with-carry or recursion-with-carry generator, invented by George Marsaglia
Algorithm:

S = 2111111111 · Xn-4 + 1492 · Xn-3 + 1776 · Xn-2 + 5115 · Xn-1 + C

Xn = S modulo 232

C = floor(S / 232)

The last four X'es and C are stored in a buffer as 32-bit unsigned integers. The intermediate S is a 64-bit unsigned integer.

The X'es and C are initialized to random values based on a seed. They cannot all be zero.

This algorithm is most effectively implemented in assembly language where you have an instruction for multiplying two 32-bit integers and get a 64-bit product. In high level languages you do not have such an instruction, so you have to use floating point numbers with a mantissa of at least 63 bits. Not all compilers support this precision, for example Microsoft Visual C++ does not.
Specifications:

* 32-bit integer output
* cycle length is 3x10^47
* very good randomness