Author: Krzysztof Aleksander Pyrkosz Last-Update: Sun, 19 Jun 2022 16:23:02 +0200 Bug-Debian: https://bugs.debian.org/1013230 Description: Improved random number generation to allow running multiple instances of the program in parallel. --- a/main.cpp +++ b/main.cpp @@ -200,9 +200,6 @@ int main(int argc, char *argv[]){ return 1; } - // Initialize the random number generator - srand ( unsigned ( time(0) ) ); - // If printing out CSV, print a header if (printStyle == SudokuBoard::CSV){ if (printPuzzle) cout << "Puzzle,"; --- a/qqwing.cpp +++ b/qqwing.cpp @@ -23,6 +23,7 @@ #include #include +#include #include "qqwing.hpp" @@ -109,6 +110,7 @@ namespace qqwing { * Create a new Sudoku board */ SudokuBoard::SudokuBoard() : + rand_generator(random_device{}()), puzzle ( new int[BOARD_SIZE] ), solution ( new int[BOARD_SIZE] ), solutionRound ( new int[BOARD_SIZE] ), @@ -1528,18 +1530,13 @@ namespace qqwing { /** * Shuffle the values in an array of integers. */ - void shuffleArray(int* array, int size){ - {for (int i=0; i dist(0, 3); + switch (dist(rand_generator)){ case 0: return SudokuBoard::ROTATE90; case 1: return SudokuBoard::ROTATE180; case 2: return SudokuBoard::MIRROR; --- a/qqwing.hpp +++ b/qqwing.hpp @@ -24,6 +24,7 @@ #include #include + #include namespace qqwing { @@ -121,6 +122,13 @@ private: /** + * Random number generator seeded with true randomness + * in the constructor. Replacement for srand(time(0)). + * Now, multiple instances of the program running + * in parallel generate different boards. + */ + std::mt19937 rand_generator; + /** * The 81 integers that make up a sudoku puzzle. * Givens are 1-9, unknowns are 0. * Once initialized, this puzzle remains as is. @@ -230,6 +238,8 @@ bool arePossibilitiesSame(int position1, int position2); void addHistoryItem(LogItem* l); void shuffleRandomArrays(); + void shuffleArray(int* array, int size); + SudokuBoard::Symmetry getRandomSymmetry(); void print(int* sudoku); void rollbackNonGuesses(); void clearPuzzle();