[英]Problems with my random number generator, numbers far out of range
我正在嘗試為學校項目制作一個數獨謎題生成器,並且我已經為該程序制定了大部分編碼和邏輯,只是我用來為數獨網格賦值的隨機數生成器正在生成數字遠遠超出了適當的范圍,我不太明白為什么它不能正常工作。 這是數字生成器的代碼:
int Sudoku::RNG(int range, int start){
int randNum;
randNum = (rand()%range+start);
return randNum;
}
我也播種了
srand(time(NULL));
在我的主要方法的一開始。
下面是使用 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);
}
}
主要方法:
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);
}
下面是錯誤的數獨板示例(由於數字很大,格式已關閉,只需忽略破折號和垂直線):
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
它給我的數字有時非常大,有時不是。 目前我有點困惑,任何幫助或建議將不勝感激!
編輯/更新#1:
我修改了我的 gridPopulate 代碼,並添加了一些用於故障排除的 cout 語句。 該方法現在將保留在每個單獨的數組元素上,直到它被填充,我現在遇到的主要問題是代碼將卡在發生 RNG() 的 do/while 語句中。 我正在使用計數器來跟蹤在將安全值傳遞到網格之前它經歷了多少次 RNG 迭代。 問題是 RNG 在生成安全值之前經歷了太多次迭代,有時在繼續之前經歷了數千次迭代。 在某些時候,它會無休止地卡住,無法繼續生成隨機數。
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));
這也是我的 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.