# Pseudorandom Number Generator

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].

# Methods

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.

## LCG

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].

## RKISS

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

## RANROT B3

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].

## MT

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

## ChaCha

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].

# Forum Posts

## 1990 ...

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

## 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