Answers.Body: <p>Computing magics at statup is fine because it's done only once and not very slow.</p>
  
  <blockquote>
    <p><a href="https://github.com/official-stockfish/Stockfish/blob/master/src/bitboard.cpp" rel="nofollow noreferrer">https://github.com/official-stockfish/Stockfish/blob/master/src/bitboard.cpp</a></p>
  </blockquote>
  
  <pre><code>// 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.
  </code></pre>
  
  <p>Stockfish did it. Leaving the constants in the code is also fine. It's all about implementation.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-28T08:47:12.790Z
Answers.Text: 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).
Answers.UserDisplayName: 123iamking
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-24T23:34:17.810Z
Answers.LastActivityDate: 2020-03-25T04:16:38.127Z
Answers.LastEditDate: 2020-03-25T04:16:38.127Z
Answers.UserDisplayName: SmallChess
Body: <p>I read about <a href="https://www.chessprogramming.org/Looking_for_Magics" rel="nofollow noreferrer">Looking for Magics</a> for Magic Bitboard, they mentioned the method "Feeding in Randoms". </p>
  
  <p>As I understand, this method works like this:</p>
  
  <pre><code>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 -&gt; success! now we have the magic for square 1, 63 more squares to go
  </code></pre>
  
  <p>The author said</p>
  
  <blockquote>
    <p>On my Core Duo 2.8 GHz, it takes less than a second to find magic numbers for rooks and bishops for all squares</p>
  </blockquote>
  
  <p>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)?</p>
  
  <p>Maybe I misunderstand something about this. So I want to ask Why have to find magic instead of just loading from disk?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-03-23T15:51:30.440Z
Comments.Text: 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.
Comments.UserDisplayName: NoseKnowsAll
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-03-23T20:29:51.587Z
Comments.Text: 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.
Comments.UserDisplayName: PhishMaster
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-03-24T22:58:32.163Z
Comments.Text: 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.
Comments.UserDisplayName: Mike Jones
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-03-24T23:00:55.163Z
Comments.Text: @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.
Comments.UserDisplayName: Mike Jones
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-03-24T23:35:34.793Z
Comments.Text: @SmallChess You are entitled to do what you want as am I. The OP is a new user, and it seems like a totally reasonable question to me, even if it is tough to answer. THAT was my main reason for upvoting (some other people can't read), and I like for new people not to feel attacked, and welcomed. I would be interested as to why you think it was a bad question, especially since you have now taken the time to actually answer it.
Comments.UserDisplayName: PhishMaster
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-03-25T04:14:05.060Z
Comments.Text: @PhishMaster Yes, it was me who downvoted it, because I thought it was silly question given that it only took like a second to compute the magic numbers.
Comments.UserDisplayName: SmallChess
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-03-25T10:55:37.987Z
Comments.Text: @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.
Comments.UserDisplayName: PhishMaster
Tags: <engines><programming><bitboard>
Title: Magic Bitboards, Feeding in Randoms - Why have to find magic instead of just loading from disk?
ThreadText: # Magic Bitboards, Feeding in Randoms - Why have to find magic instead of just loading from disk?
  
  (Asked by: username_0 on 2020-03-23)
  
  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?
  
  **Tags:** engines, programming, bitboard
  
  > **Comments:**
  > **username_1**: 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.  
  > **username_2**: 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.  
  > **username_3**: 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.  
  > **username_3**: @username_2  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.  
  > **username_2**: @username_4 You are entitled to do what you want as am I. The OP is a new user, and it seems like a totally reasonable question to me, even if it is tough to answer. THAT was my main reason for upvoting (some other people can't read), and I like for new people not to feel attacked, and welcomed. I would be interested as to why you think it was a bad question, especially since you have now taken the time to actually answer it.  
  > **username_4**: @username_2 Yes, it was me who downvoted it, because I thought it was silly question given that it only took like a second to compute the magic numbers.  
  > **username_2**: @username_4 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.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_4 on 2020-03-24. Score: 1 | Accepted answer
  
  Computing magics at statup is fine because it's done only once and not very slow.
  
  https://github.com/official-stockfish/Stockfish/blob/master/src/bitboard.cpp
  
  // 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.
  
  > **Comments:**
  > **username_0**: 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).  
  
  ---
  
  