vendredi 16 août 2019

Random generating of numbers doesn't work properly

I'm trying to create a programm that makes sudoku's. But when I try to let the programm place numbers at random spots it doesnt use every position.

I tried to use rand(); with srand(time(0)); and randomnumbergenerators from random

In the Constructor i use this:

mt19937_64 randomGeneratorTmp(time(0));
randomGenerator = randomGeneratorTmp;
uniform_int_distribution<int> numGetterTmp(0, 8);
numGetter = numGetterTmp;

While I have randomGenerator and numGetter variable so i can use them in another function of the sudoku object. And this is the function where i use the random numbers:

bool fillInNumber(int n){
     int placedNums = 0, tries=0;
     int failedTries[9][9];
     for(int dim1=0;dim1<9;dim1++){
         for(int dim2=0;dim2<9;dim2++){
             failedTries[dim1][dim2] = 0;
         }
     }

     while(placedNums<9){
         int dim1 = numGetter(randomGenerator);
         int dim2 = numGetter(randomGenerator);
         if(nums[dim1][dim2]==0){
             if(allowedLocation(n,dim1,dim2)){
                 nums[dim1][dim2] = n;
                 placedNums++;
             } else {
                 failedTries[dim1][dim2]++;
                 tries++;
             }
         }
         if(tries>100000000){
             if(placedNums == 8){
                 cout<< "Number: " << n << endl;
                 cout<< "Placing number: " << placedNums << endl;
                 cout<< "Dim1: " << dim1 << endl;
                 cout<< "Dim2: " << dim2 << endl;
                 printArray(failedTries);
             }
             return false;
         }
     }
     return true;
}

( The array "failedTries" just shows me which positions the programm tried. and most of the fields have been tried millions of times, while others not once)

I think that the random generation just repeats itself before it used every numbercombination, but i dont know what im doing wrong.




Aucun commentaire:

Enregistrer un commentaire