Pseudorandom Number Generator

From Chessprogramming wiki
Jump to: navigation, search

Home * Programming * Algorithms * Pseudorandom Number Generator

Dice players [1]

Pseudorandom Number Generator (PRNG),
an algorithmic gambling device for generating pseudorandom numbers, a deterministic sequence of numbers which appear to be random with the property of reproducibility. They are useful in simulation, sampling, computer programming, decision making, cryptography, aesthetics and recreation [2] - in computer chess, beside randomization of game playing, primarily used to generate keys for Zobrist hashing.

Games of chance, such as Backgammon and EinStein würfelt nicht! require PRNGs for rolling a dice. Chess and game playing programs use PRNGs to randomly choose one of several possible moves of a position from an opening book, or use random fluctuations in learning and automated tuning tasks. Game playing programs performing a Monte-Carlo Tree Search, use random playouts in their simulation phase. Don Beal and Martin C. Smith demonstrated that deeper search with random leaf values in chess yields to better play, with some interesting insights in minimax search and implications for designing and testing evaluation terms [3].


PRNGs maintain a state variable, a bitwise superset of the random number, initialized by a random seed - a constant for the same sequence each time, i.e. for Zobrist keys, otherwise, something like system time to produce varying pseudo randoms. Each call of the PRNG routine performs certain operations or bit-twiddling on that state, leaving the next pseudo random number. For various applications more or less important features are randomness, resolution, (uniform) distribution, and periodicity - topic of various randomness tests - and further performance, and portability.

Zobrist hashing with about 12*64 keys has less issues with randomness and period, but with distribution, and requires linear independence so that a small subset of all keys doesn't xor to zero. Despite selected hard-coded random constants used at compile time, many programs use an own PRNG based on recurrence relation in GF(2) such as Mersenne Twister or Xorshift.


A common method used in many library functions, such as C/C++ rand() is the linear congruential generator (LCG) based on multiply, add, modulo with integers, where some past implementations had serious shortcomings in the randomness, distribution and period of the sequence [4]. Due to such issues in rand() implementations, where lower bits are less random than higher bits, it is recommended not to use modulo X to reduce the integer range from RAND_MAX to X, but division by (RAND_MAX div X) [5] - or to use C++11's random number generation facilities to replace rand [6].


Stockfish (since 2.0) uses an implementation by Heinz van Saanen based on Bob Jenkins' RKISS, a member of George Marsaglia's Xorshift familly.


Amy by Thorsten Greiner [7] uses an implementation of Agner Fog's RANROT B3 [8], also recommended by Stefan Meyer-Kahlen as used in Shredder [9].


Arasan 20.x switched to C++11 random number support using Mersenne Twister std::mt19937_64 [10] [11].


The Rust engine Asymptote uses the ChaCha stream cipher developed by Daniel J. Bernstein, built on a pseudorandom function based on add-rotate-xor (ARX) operations [12].


See also

Selected Publications

1950 ...

1960 ...

1970 ...

1980 ...

1990 ...

2000 ...

2010 ...

Forum Posts

1982 ...

1990 ...

Re: Hash tables - Clash!!! What happens next? by Jonathan Schaeffer, March 17, 1994

1995 ...

2000 ...

Re: random book moves/ random generator by Thorsten Greiner, CCC, January 13, 2000
Re: About random numbers and hashing by Sven Reichard, CCC, December 05, 2001

2005 ...

2010 ...

2015 ...

2020 ...

External Links



Lehmer random number generator from Wikipedia
Park-Miller-Carta Pseudo-Random Number Generators
RANDU from Wikipedia
Mersenne Twister - Home Page

Language Support















  1. Dice players - Roman fresco from the Osteria della Via di Mercurio (VI 10,1.19, room b) in Pompeii, Source: Filippo Coarelli (ed.) (2002). Pompeji. Hirmer Verlag, Munich, ISBN: 3-7774-9530-1, p. 146, Wikimedia Commons, History of randomness from Wikipedia
  2. Donald Knuth (1997). The Art of Computer Programming (TAOCP) - Volume 2 - Seminumerical Algorithms. Chapter 3 – Random numbers
  3. Don Beal, Martin C. Smith (1994). Random Evaluations in Chess. Advances in Computer Chess 7
  4. "In the past, some implementations of rand() have had serious shortcomings in the randomness, distribution and period of the sequence produced (in one well-known example, the low-order bit simply alternated between 1 and 0 between calls) rand() is not recommended for serious random-number generation needs, like cryptography. It is recommended to use C++11's random number generation facilities to replace rand()." rand -
  5. Re: random book moves/ random generator by Thorsten Greiner, CCC, January 13, 2000
  6. Pseudo-random number generation -
  7. amy-0.8.7.tar.gz /src/random.c
  8. Agner Fog (2001). Chaotic Random Number Generators with Random Cycle Lengths. pdf
  9. Re: random book moves/ random generator by Stefan Meyer-Kahlen, CCC, January 13, 2000
  10. arasan-chess/movegen.h at master · jdart1/arasan-chess · GitHub
  11. mt19937_64 - C++ Reference
  12. rand::chacha::ChaChaRng - Rust
  13. Salsa20 from Wikipedia
  14. Mersenne twister from Wikipedia

Up one Level