refactor(official-bots): use java.util.Random in PolyglotBook
Build & Test (NowChessSystems) TeamCity build finished
Build & Test (NowChessSystems) TeamCity build finished
scala.util.Random delegates to a shared global java.util.Random, a contention point across concurrent bot games. Use a per-book java.util.Random instance instead. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -5,8 +5,8 @@ import de.nowchess.api.game.GameContext
|
|||||||
import de.nowchess.api.move.{Move, MoveType, PromotionPiece}
|
import de.nowchess.api.move.{Move, MoveType, PromotionPiece}
|
||||||
|
|
||||||
import java.io.{DataInputStream, FileInputStream, InputStream}
|
import java.io.{DataInputStream, FileInputStream, InputStream}
|
||||||
|
import java.util.Random
|
||||||
import scala.collection.mutable
|
import scala.collection.mutable
|
||||||
import scala.util.Random
|
|
||||||
|
|
||||||
/** Reads a Polyglot opening book (.bin file) and probes it for moves.
|
/** Reads a Polyglot opening book (.bin file) and probes it for moves.
|
||||||
*
|
*
|
||||||
@@ -18,6 +18,8 @@ import scala.util.Random
|
|||||||
*/
|
*/
|
||||||
final class PolyglotBook private (entries: Map[Long, Vector[BookEntry]]):
|
final class PolyglotBook private (entries: Map[Long, Vector[BookEntry]]):
|
||||||
|
|
||||||
|
private val rng = Random()
|
||||||
|
|
||||||
/** Probe the book for a move in the given position. Returns a weighted random move, or None if not in book. */
|
/** Probe the book for a move in the given position. Returns a weighted random move, or None if not in book. */
|
||||||
def probe(context: GameContext): Option[Move] =
|
def probe(context: GameContext): Option[Move] =
|
||||||
val hash = PolyglotHash.hash(context)
|
val hash = PolyglotHash.hash(context)
|
||||||
@@ -93,7 +95,7 @@ final class PolyglotBook private (entries: Map[Long, Vector[BookEntry]]):
|
|||||||
if entries.length == 1 then entries.head
|
if entries.length == 1 then entries.head
|
||||||
else
|
else
|
||||||
val totalWeight = entries.map(_.weight).sum
|
val totalWeight = entries.map(_.weight).sum
|
||||||
val pick = Random.nextInt(totalWeight.max(1)) // NOSONAR
|
val pick = rng.nextInt(totalWeight.max(1)) // NOSONAR
|
||||||
|
|
||||||
@scala.annotation.tailrec
|
@scala.annotation.tailrec
|
||||||
def select(remaining: Int, idx: Int): BookEntry =
|
def select(remaining: Int, idx: Int): BookEntry =
|
||||||
|
|||||||
Reference in New Issue
Block a user