Debian Codegraph @

debian/patches/random_number_generation_fixes.patch

raw
1 Author: Krzysztof Aleksander Pyrkosz <[email protected]>
2 Last-Update: Sun, 19 Jun 2022 16:23:02 +0200
3 Bug-Debian: https://bugs.debian.org/1013230
4 Description: Improved random number generation to allow running multiple instances of the program in parallel.
5
6 --- a/main.cpp
7 +++ b/main.cpp
8 @@ -200,9 +200,6 @@ int main(int argc, char *argv[]){
9 return 1;
10 }
11
12 - // Initialize the random number generator
13 - srand ( unsigned ( time(0) ) );
14 -
15 // If printing out CSV, print a header
16 if (printStyle == SudokuBoard::CSV){
17 if (printPuzzle) cout << "Puzzle,";
18 --- a/qqwing.cpp
19 +++ b/qqwing.cpp
20 @@ -23,6 +23,7 @@
21
22 #include <cstdlib>
23 #include <iostream>
24 +#include <algorithm>
25
26 #include "qqwing.hpp"
27
28 @@ -109,6 +110,7 @@ namespace qqwing {
29 * Create a new Sudoku board
30 */
31 SudokuBoard::SudokuBoard() :
32 + rand_generator(random_device{}()),
33 puzzle ( new int[BOARD_SIZE] ),
34 solution ( new int[BOARD_SIZE] ),
35 solutionRound ( new int[BOARD_SIZE] ),
36 @@ -1528,18 +1530,13 @@ namespace qqwing {
37 /**
38 * Shuffle the values in an array of integers.
39 */
40 - void shuffleArray(int* array, int size){
41 - {for (int i=0; i<size; i++){
42 - int tailSize = size-i;
43 - int randTailPos = rand()%tailSize+i;
44 - int temp = array[i];
45 - array[i] = array[randTailPos];
46 - array[randTailPos] = temp;
47 - }}
48 + void SudokuBoard::shuffleArray(int* array, int size){
49 + std::shuffle(array, array + size, rand_generator);
50 }
51
52 - SudokuBoard::Symmetry getRandomSymmetry(){
53 - switch (rand()%4){
54 + SudokuBoard::Symmetry SudokuBoard::getRandomSymmetry(){
55 + std::uniform_int_distribution<int> dist(0, 3);
56 + switch (dist(rand_generator)){
57 case 0: return SudokuBoard::ROTATE90;
58 case 1: return SudokuBoard::ROTATE180;
59 case 2: return SudokuBoard::MIRROR;
60 --- a/qqwing.hpp
61 +++ b/qqwing.hpp
62 @@ -24,6 +24,7 @@
63
64 #include <string>
65 #include <vector>
66 + #include <random>
67
68 namespace qqwing {
69
70 @@ -121,6 +122,13 @@
71
72 private:
73 /**
74 + * Random number generator seeded with true randomness
75 + * in the constructor. Replacement for srand(time(0)).
76 + * Now, multiple instances of the program running
77 + * in parallel generate different boards.
78 + */
79 + std::mt19937 rand_generator;
80 + /**
81 * The 81 integers that make up a sudoku puzzle.
82 * Givens are 1-9, unknowns are 0.
83 * Once initialized, this puzzle remains as is.
84 @@ -230,6 +238,8 @@
85 bool arePossibilitiesSame(int position1, int position2);
86 void addHistoryItem(LogItem* l);
87 void shuffleRandomArrays();
88 + void shuffleArray(int* array, int size);
89 + SudokuBoard::Symmetry getRandomSymmetry();
90 void print(int* sudoku);
91 void rollbackNonGuesses();
92 void clearPuzzle();