I read about Looking for Magics for Magic Bitboard, they mentioned the method "Feeding in Randoms".

As I understand, this method works like this:

label RETRY;
magic = random();//trial

for (all cases of current square: from first case to last case)
  if (magic fails at any case)
    go to label RETRY;//and error
//Passed all cases -> success! now we have the magic for square 1, 63 more squares to go

The author said

On my Core Duo 2.8 GHz, it takes less than a second to find magic numbers for rooks and bishops for all squares

If we successfully get magic numbers with this "Randoms" method, why not just save the magic numbers to disk and load it from disk instead of running the "Feeding in Randoms" code every time at startup (that the author cares about how fast this method, isn't it faster just read the magic from disk instead of trial and error with random. If load from disk, it doesn't matter if the method takes whole day to find magic)?

Maybe I misunderstand something about this. So I want to ask Why have to find magic instead of just loading from disk?

  • I don't truly understand much about these magic numbers, but on current architectures it's a truth that memory movement is incredibly slow relative to the speed of number crunching or computation time. So in general, loading anything from disk would be impossibly slow relative to nearly any other method of computing those results. Mar 23, 2020 at 15:51
  • 1
    I am not even remotely qualified to answer this as I am not a software engineer, but +1 to make up for the downvote since there is no valid reason for it that they were willing to own up to. Mar 23, 2020 at 20:29
  • 1
    Since this is a one time action, the time involved shouldn't even be a consideration. Loading the 128 numbers is easier (and probable faster) than computing them at startup.
    – Mike Jones
    Mar 24, 2020 at 22:58
  • 2
    @PhishMaster So your whole justification for an up vote is because you perceive the OP was wrongfully down voted? Talk about not having a valid reason.
    – Mike Jones
    Mar 24, 2020 at 23:00
  • 1
    @SmallChess It is simple for you, but to someone, who does not know, it is not simple. Think about the simple pure chess questions that pop up here, like yesterday, they guy who asked why in the Ragozin (he did not know it was the Ragozin) Qa4+ did not win the Bb4, when the simple Nc6 saved it. There are no silly questions when you do not know, and are trying to be genuine. Of course, you can have people, who try to be that way on purpose, but I did not sense that was the case. I really hate the downvote system here as it is really just so negative beyond the -2 points. Mar 25, 2020 at 10:55

1 Answer 1


Computing magics at statup is fine because it's done only once and not very slow.


// init_magics() computes all rook and bishop attacks at startup. Magic
// bitboards are used to look up attacks of sliding pieces. As a reference see
// www.chessprogramming.org/Magic_Bitboards. In particular, here we use the so
// called "fancy" approach.

Stockfish did it. Leaving the constants in the code is also fine. It's all about implementation.

  • I see, I've successfully implemented the const approach, works great and easier to understand (I prefer less & solid code). I personally against the computing magics at startup because it will take time to read & understand that computing for other people (or me 3 months later).
    – 123iamking
    Mar 28, 2020 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.