diff --git a/source/datagen.cpp b/source/datagen.cpp index 38d1620..88b56f6 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -9,6 +9,7 @@ #include "evaluate.h" #include "search.h" #include "movegen.h" +#include "utils.h" #ifdef _WIN32 #include @@ -59,17 +60,12 @@ static bool IsGameOver(Board &board, SEARCH::SearchContext& ctx) { } static void PlayRandMoves(Board &board, SEARCH::SearchContext& ctx) { - std::random_device dev; - std::mt19937_64 rng(dev()); - - std::uniform_int_distribution moveDist(0, 1); - bool plusOne = moveDist(rng); + bool plusOne = UTILS::RandomBool(); for (int count = 0; count < RAND_MOVES + plusOne; count++) { MOVEGEN::GenerateMoves(board); - std::uniform_int_distribution dist(0, board.currentMoveIndex - 1); - bool isLegal = board.MakeMove(board.moveList[dist(rng)]); + bool isLegal = board.MakeMove(board.moveList[UTILS::RandomInt(0, board.currentMoveIndex - 1)]); if (!isLegal) { count--; @@ -79,6 +75,8 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext& ctx) { MOVEGEN::GenerateMoves(board); if (IsGameOver(board, ctx)) break; } + + board.PrintBoard(); } static void WriteToFile(std::vector &gamesBuffer, std::ofstream &file) { diff --git a/source/utils.cpp b/source/utils.cpp index 4a5bb6f..b5588bb 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -8,35 +8,51 @@ namespace UTILS { U64 initialSeed = 0x60919C48E57863B9; // PRNG using xorshift -static U64 RandomNum() { +static U64 RandomU64() { initialSeed ^= initialSeed << 13; initialSeed ^= initialSeed >> 7; initialSeed ^= initialSeed << 17; return initialSeed; } +bool RandomBool() { + return (RandomU64() & 1) == 1; +} + +int RandomInt(int min, int max) { + if (max < min) { + int temp = min; + min = max; + max = temp; + } + + int range = max - min + 1; + + return min + (RandomU64() % range); +} + void InitZobrist() { // init piece square keys for (int i = White; i <= Black; i++) { for (int j = Pawn; j <= King; j++) { for (int k = a1; k <= h8; k++) { - zKeys[i][j][k] = RandomNum(); + zKeys[i][j][k] = RandomU64(); } } } // init en passant file keys for (int i = 0; i < 8; i++) { - zEnPassant[i] = RandomNum(); + zEnPassant[i] = RandomU64(); } // init castling rights keys for (int i = 0; i < 16; i++) { - zCastle[i] = RandomNum(); + zCastle[i] = RandomU64(); } // init side key - zSide = RandomNum(); + zSide = RandomU64(); } U64 GetHashKey(Board &board) { diff --git a/source/utils.h b/source/utils.h index 37aa02b..df298cc 100644 --- a/source/utils.h +++ b/source/utils.h @@ -21,6 +21,9 @@ inline U64 zCastle[16]; inline U64 zSide; +bool RandomBool(); +int RandomInt(int min, int max); + void InitZobrist(); U64 GetHashKey(Board &board);