Devlog 0x1 - Pseudorandom Number Generator
Devlog 0x1. This edition covers my implementation of a pseudo random number generator using the XORSHIFT32 algorithm.
Pseudo Random Number Generation
First, a bit about magic bitboards. Magic Bitboards are an optimization technique used in chess engines to quickly calculate piece movement possiblities. They use pre-computed lookup tables and bitwise operations to determine valid moves. Magic numbers are special numbers that when multiplied by the occupied squares bitboard, cretes unique indicies. For this approach to work properly, I am using the following initial state: 1804289383. This initial state has been used in several other engine implementations, here is a video on implementing this algorithm by Maxim Korzh and here an article on finding magic numbers.
Implementation of this PRNG was quite simple. The algorithm is simply a few bit shifts on an initial state. We'll begin by defining the initial state within the bitboard.zig file:
pub var state: u32 = 1804289383;
And then we perform the XORSHIFT32 algorithm on this initial state:
fn getRandomNumber() u32 {
var number: u32 = bitboard.state;
number ^= number << 13;
number ^= number >> 17;
number ^= number << 5;
bitboard.state = number;
return number;
}
Thanks for reading, see you tomorrow!