简体   繁体   English

我的随机数生成器出现问题,数字超出范围

[英]Problems with my random number generator, numbers far out of range

I'm trying to make a Sudoku puzzle generator for a school project, and I have most of the coding and logic worked out for the program, it's just that the random number generator I'm using to assign values to the Sudoku grid are generating numbers far out of the appropriate range, and I can't quite figure out why it's not functioning appropriately.我正在尝试为学校项目制作一个数独谜题生成器,并且我已经为该程序制定了大部分编码和逻辑,只是我用来为数独网格赋值的随机数生成器正在生成数字远远超出了适当的范围,我不太明白为什么它不能正常工作。 Here's the code for the number generator:这是数字生成器的代码:

int Sudoku::RNG(int range, int start){
    int randNum;
    randNum = (rand()%range+start);
    return randNum;
}

I've also seeded with我也播种了

srand(time(NULL)); 

at the very start of my main method.在我的主要方法的一开始。

And here's the code for the method which populates the Sudoku grid with numbers from the RNG() function:下面是使用 RNG() function 中的数字填充数独网格的方法的代码:

void Sudoku::gridPopulate(int grid [9][9]){
    Solution s;
    int num;
    bool safe;
    for(int i=0;i<9;i++){
        for(int j=0;j<9;j++){
            do{
                safe = false;
                num= RNG(9,1);
                if(s.rowCheck(grid, i, num)&& s.colCheck(grid, j, num) && s.gridCheck(grid, i, j, num)){
                    grid[i][j] = num;
                    cout << "Number entered into grid" << endl;
                    safe = true;
                }
            }while(safe);
        }
    }

Main Method:主要方法:

int main()
{
    //Program Start
    srand(time(NULL));
    int difficulty;
    int choice;
    int grid[9][9];

    cout << "Welcome to my Sudoku puzzle generator! \nTest your mental muscles and see if you can solve the puzzle!" << endl;

    //Puzzle Generator
    Sudoku game;
    game.gridPopulate(grid);
    cout << "board populated"<< endl;
    cout << "Successful board made" << endl;
    game.displayBoard(grid);
}

Example of erroneous Sudoku board below (formatting is off because of the huge numbers, just ignore the dashes and vertical lines):下面是错误的数独板示例(由于数字很大,格式已关闭,只需忽略破折号和垂直线):

1  5013192 4981028 9 |20064 8 5 |1 2 3 |
2  8 5 4 |1995768265 0 2114351727 |28 7670880 7670908 |
3  1 7274056 7 |3 0 7670880 |4 6 7670880 |
   -----------------4  6 7 2 |4199040 1 8 |7670912 -1196314433 4 |
5  1953657218 1 0 |5 4 32 |7 3 7274140 |
6  5 8 1953722109 |2 9 7670916 |7274116 4199040 4358512 |
   -----------------7  0 1953722297 1 |9 1953787893 3 |7274204 4 8 |
8  1953722109 1953722083 8 |4199040 4199040 0 |9 7274160 2 |
9  3 1953746112 1198757840 |6 7274216 4 |4358512 7274368 4358606 |
   -----------------

   1 2 3 4 5 6 7 8 9

The number's it's giving me are sometimes very large, sometimes not.它给我的数字有时非常大,有时不是。 It's kinda baffling me at the moment, any help or advice would be greatly appreciated!目前我有点困惑,任何帮助或建议将不胜感激!

EDIT/UPDATE #1:编辑/更新#1:

I've revised my gridPopulate code, and I've added in a few cout statements for troubleshooting.我修改了我的 gridPopulate 代码,并添加了一些用于故障排除的 cout 语句。 The method will now stay on each individual array element until it is populated, the main issue I'm running into now is that the code will get stuck in the do/while statement where the RNG() is occurring.该方法现在将保留在每个单独的数组元素上,直到它被填充,我现在遇到的主要问题是代码将卡在发生 RNG() 的 do/while 语句中。 I'm using the counter to track how many RNG iterations it goes through before passing a safe value into the grid.我正在使用计数器来跟踪在将安全值传递到网格之前它经历了多少次 RNG 迭代。 The problem is that the RNG is going through way too many iterations before happening to generate a safe value, sometimes thousands of iterations before moving on.问题是 RNG 在生成安全值之前经历了太多次迭代,有时在继续之前经历了数千次迭代。 At some point it will just get stuck endlessly generating random numbers without being able to move on.'在某些时候,它会无休止地卡住,无法继续生成随机数。

   void Sudoku::gridPopulate(int grid [9][9]){
        Solution s;
        int num;

        do{
            for(int i=0;i<9;i++){
                for(int j=0;j<9;j++){
                    int counter = 0;
                    do{
                        srand(time(NULL));
                        num = RNG(9,1);
                        counter++;
                        cout << "RNG attempts: " << counter << endl;
                    }while(!(s.rowCheck(grid, i, num)&& s.colCheck(grid, j, num) && s.gridCheck(grid, i, j, num)));
                grid[i][j]=num;
                cout << grid[i][j] << endl;
                cout << "Row #: " << i << endl;
                cout << "Col #: " << j << endl;
                }//end j loop
                displayBoard(grid);
            }//end i loop
            displayBoard(grid);
        }while(!validityCheck(grid));

Here's my displayBoard method too, though I don't think it is correlated to the problem.这也是我的 displayBoard 方法,但我认为它与问题无关。

void Sudoku::displayBoard(int grid[9][9]){
    /**********************************************************/
    cout << "   ----------------------" << endl;
    for(int i=0; i<9;i++){
        if(i==0)
            cout << "1  | ";
        if(i==1)
            cout << "2  | ";
        if(i==2)
            cout << "3  | ";
        if(i==3)
            cout << "4  | ";
        if(i==4)
            cout << "5  | ";
        if(i==5)
            cout << "6  | ";
        if(i==6)
            cout << "7  | ";
        if(i==7)
            cout << "8  | ";
        if(i==8)
            cout << "9  | ";
        for(int j=0;j<9;j++){
            cout << grid[i][j] << " ";
            if(j==2)
            cout << "| ";
            if(j==5)
            cout << "| ";
            if(j==8)
            cout << "| ";
        }
        cout << endl;
        if(i==2)
            cout << "   ------------------------" << endl;
        if(i==5)
            cout << "   ------------------------" << endl;
        if(i==8)
            cout << "   ------------------------" << endl;
    }
    cout << "\n";
    cout << "     1 2 3   4 5 6   7 8 9 " << endl;

    /**********************************************************/
}
Nothing wrong with the RNG function, all we need is debugging skills
## Try this to see what is the immediate value of the grid[i][j] ##
`void Sudoku::void gridPopulate(int grid[9][9]) 
{
        Solution s;
        int num;
        bool safe;
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                do {
                    safe = false;
                    num = RNG(9, 1);
                    if (s.rowCheck(grid, i, num) && s.colCheck(grid, j, num) && s.gridCheck(grid, i, j, num)) {
                        grid[i`enter code here`][j] = num;
                        //cout << "Number entered into grid" << endl;
                        safe = true;
                    }
                } while (safe);
                cout << grid[i][j] << " ";
            }
            cout << endl;
        }
    }`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM