Zobrist keys
Moderator: Ras
-
- Posts: 22
- Joined: Fri May 30, 2025 10:18 pm
- Full name: Ben Vining
Zobrist keys
Most descriptions of Zobrist hashing I've found say that the keys are randomly generated at program startup. Why generate them each time the program runs, why not just use some static constants? Is there a downside to using hard-coded keys?
-
- Posts: 1631
- Joined: Thu Jul 16, 2009 10:47 am
- Location: Almere, The Netherlands
Re: Zobrist keys
Using static or compile-time constants for the Zobrist keys is fine (besides making the executable somewhat larger on disk).
-
- Posts: 28353
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: Zobrist keys
Note that reading them from disk as part of the executable's initialized data is probably much slower than having the computer calculate them.
-
- Posts: 1273
- Joined: Wed Mar 08, 2006 8:28 pm
- Location: Florida, USA
Re: Zobrist keys
I use the Polyglot Zobrist keys in my engine. This has the added advantage of making it really easy for your engine to use Polyglot opening books. You can find the keys here:
http://hgm.nubati.net/book_format.html
— Steve
http://hgm.nubati.net/book_format.html
— Steve
http://www.chessprogramming.net - Juggernaut & Maverick Chess Engine
-
- Posts: 125
- Joined: Sat Jul 30, 2022 12:12 pm
- Full name: Jamie Whiting
Re: Zobrist keys
No engine should (and I am not aware of a single decent engine that does) generate different zobrist keys at each startup, it should use a fixed seed and fixed prng. This is because you want the ability to make a change to some code, and verify that it is nonfunctional in search (on a single thread) which would be immediately impossible if each time you run the engine you have different zobrist values. An example of where this is valuable is when you perform large refactors.
Whether you hard code these values or (re)generate them at startup is just preference.
Edit:
Also, pretty much every strong engine implements `bench`(see here https://github.com/AndyGrant/OpenBench/ ... quirements) as a checksum for distributed testing via OpenBench as linked or Fishtest in the case of Stockfish, that directly relies on engines being deterministic on a single thread across multiple runs.
-
- Posts: 22
- Joined: Fri May 30, 2025 10:18 pm
- Full name: Ben Vining
-
- Posts: 5693
- Joined: Tue Feb 28, 2012 11:56 pm
Re: Zobrist keys
But if you run the engine more than once, the executable will usually be cached in RAM, and calculating the Zobrist keys will be a huge slowdown (measured in attoseconds).
-
- Posts: 28353
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: Zobrist keys
-
- Posts: 28353
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: Zobrist keys
Well, even reading the keys from RAM is probably slower than recalculating them. And in a test with fast TC you would probably not reload the engine for every game. (And you would calculate the keys only on loading, not for every new game.)
-
- Posts: 5693
- Joined: Tue Feb 28, 2012 11:56 pm
Re: Zobrist keys
Your engines don't have an array of Zobrist keys in RAM?hgm wrote: ↑Sun Jun 08, 2025 7:52 amWell, even reading the keys from RAM is probably slower than recalculating them. And in a test with fast TC you would probably not reload the engine for every game. (And you would calculate the keys only on loading, not for every new game.)