Hackers News

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!

admin

The realistic wildlife fine art paintings and prints of Jacquie Vaux begin with a deep appreciation of wildlife and the environment. Jacquie Vaux grew up in the Pacific Northwest, soon developed an appreciation for nature by observing the native wildlife of the area. Encouraged by her grandmother, she began painting the creatures she loves and has continued for the past four decades. Now a resident of Ft. Collins, CO she is an avid hiker, but always carries her camera, and is ready to capture a nature or wildlife image, to use as a reference for her fine art paintings.

Related Articles

Leave a Reply