Answers.Body: <p><strong>Note:</strong></p>
  
  <p>It's a <strong>good</strong> idea for your engine be deterministic. There is no reason to select a move randomly. You would only do it to lower your engine strength. If you make your engine non-deterministic, you will find it very hard to debug and reproduce a bug. My recommendation is <strong>not</strong> to do it.</p>
  
  <p>You shouldn't modify your engine. You may want to use an opening book, and pick a line randomly. Many chess GUI can do that, for example, the <a href="https://github.com/cutechess/cutechess" rel="nofollow noreferrer">CuteChess</a> software. Arena GUI can do also that.</p>
  
  <p><strong>But if you insist:</strong></p>
  
  <p>Let's take a look at the <a href="https://github.com/official-stockfish/Stockfish/blob/master/src/search.cpp" rel="nofollow noreferrer">Stockfish source code</a>.</p>
  
  <pre><code> Move Skill::pick_best(size_t multiPV) {
  
      const RootMoves&amp; rootMoves = Threads.main()-&gt;rootMoves;
      static PRNG rng(now()); // PRNG sequence should be non-deterministic
  
      // RootMoves are already sorted by score in descending order
      Value topScore = rootMoves[0].score;
      int delta = std::min(topScore - rootMoves[multiPV - 1].score, PawnValueMg);
      int weakness = 120 - 2 * level;
      int maxScore = -VALUE_INFINITE;
  
      // Choose best move. For each move score we add two terms, both dependent on
      // weakness. One is deterministic and bigger for weaker levels, and one is
      // random. Then we choose the move with the resulting highest score.
      for (size_t i = 0; i &lt; multiPV; ++i)
      {
          // This is our magic formula
          int push = (  weakness * int(topScore - rootMoves[i].score)
                      + delta * (rng.rand&lt;unsigned&gt;() % weakness)) / 128;
  
          if (rootMoves[i].score + push &gt; maxScore)
          {
              maxScore = rootMoves[i].score + push;
              best = rootMoves[i].pv[0];
          }
      }
  
      return best;   }
  </code></pre>
  
  <ul>
  <li>Stockfish starts multi-PV (not supported in TSCP, so you'll need to code it yourself!)</li>
  <li>Compute a random number and add it to the score for each PV</li>
  <li>Compare each weighted PV, and pick the best one</li>
  </ul>
  
  <p>You may find the following links useful:</p>
  
  <ul>
  <li><a href="https://chess.stackexchange.com/questions/13026/chess-programs-that-mimic-some-amateurs">Chess programs that mimic some amateurs</a></li>
  <li><a href="https://chess.stackexchange.com/questions/7690/how-does-a-chess-engine-mimic-an-elo-rating">How does a chess engine mimic an Elo rating</a></li>
  </ul>
  
  <p><strong>Recommendation:</strong> Don't do it unless you want to weaken your engine.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-12T00:48:39.050Z
Answers.Text: the book approach worked great, thanks! I have posted a follow-up question regarding transposition tables and triangular arrays.
Answers.UserDisplayName: nak3c
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-18T02:29:18.373Z
Answers.LastActivityDate: 2017-01-18T02:37:47.037Z
Answers.LastEditDate: 2017-04-13T12:48:53.737Z
Answers.UserDisplayName: SmallChess
Body: <p>Just finished writing customizing a chess engine by basically adding a bitboard to the freely availably tscp engine. Now I'm testing it out in winboard and notice that often the two machines will play the same game multiple times in a match. I'd like to add some variety to the games by at least having it pick equal moves at random. I'm just using alpha-beta search with simple move ordering. I'm thinking about just adding a small random number to the leaf nodes to break ties but I don't really like this solution because some of the smaller bonuses that the evaluation function uses are 3-5 centipawns. So I don't have enough "room" for random number to break the ties.</p>
  
  <p>my other thought was to have the engine pick randomly which move to keep when it found an evaluation == alpha. I don't really like this because I suspect it favors moves ordered last in the search.</p>
  
  <p>Question is how can I pick from the equal scoring leaf nodes randomly? and also evenly?</p>
  
Tags: <engines>
Title: engines keep playing same game
ThreadText: # engines keep playing same game
  
  (Asked by: username_0 on 2017-01-18)
  
  Just finished writing customizing a chess engine by basically adding a bitboard to the freely availably tscp engine. Now I'm testing it out in winboard and notice that often the two machines will play the same game multiple times in a match. I'd like to add some variety to the games by at least having it pick equal moves at random. I'm just using alpha-beta search with simple move ordering. I'm thinking about just adding a small random number to the leaf nodes to break ties but I don't really like this solution because some of the smaller bonuses that the evaluation function uses are 3-5 centipawns. So I don't have enough "room" for random number to break the ties.
  
  my other thought was to have the engine pick randomly which move to keep when it found an evaluation == alpha. I don't really like this because I suspect it favors moves ordered last in the search.
  
  Question is how can I pick from the equal scoring leaf nodes randomly? and also evenly?
  
  **Tags:** engines
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-01-18. Score: 7 | Accepted answer
  
  Note:
  It's a good idea for your engine be deterministic. There is no reason to select a move randomly. You would only do it to lower your engine strength. If you make your engine non-deterministic, you will find it very hard to debug and reproduce a bug. My recommendation is not to do it.
  You shouldn't modify your engine. You may want to use an opening book, and pick a line randomly. Many chess GUI can do that, for example, the CuteChess software. Arena GUI can do also that.
  But if you insist:
  Let's take a look at the Stockfish source code.
   Move Skill::pick_best(size_t multiPV) {
  
      const RootMoves& rootMoves = Threads.main()->rootMoves;
      static PRNG rng(now()); // PRNG sequence should be non-deterministic
  
      // RootMoves are already sorted by score in descending order
      Value topScore = rootMoves[0].score;
      int delta = std::min(topScore - rootMoves[multiPV - 1].score, PawnValueMg);
      int weakness = 120 - 2 * level;
      int maxScore = -VALUE_INFINITE;
  
      // Choose best move. For each move score we add two terms, both dependent on
      // weakness. One is deterministic and bigger for weaker levels, and one is
      // random. Then we choose the move with the resulting highest score.
      for (size_t i = 0; i < multiPV; ++i)
      {
          // This is our magic formula
          int push = (  weakness * int(topScore - rootMoves[i].score)
                      + delta * (rng.rand() % weakness)) / 128;
  
          if (rootMoves[i].score + push > maxScore)
          {
              maxScore = rootMoves[i].score + push;
              best = rootMoves[i].pv[0];
          }
      }
  
      return best;   }
  
  
  Stockfish starts multi-PV (not supported in TSCP, so you'll need to code it yourself!)
  Compute a random number and add it to the score for each PV
  Compare each weighted PV, and pick the best one
  
  You may find the following links useful:
  
  Chess programs that mimic some amateurs
  How does a chess engine mimic an Elo rating
  
  Recommendation: Don't do it unless you want to weaken your engine.
  
  > **Comments:**
  > **username_0**: the book approach worked great, thanks! I have posted a follow-up question regarding transposition tables and triangular arrays.  
  
  ---
  
  