#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main ()
{
    int arr[9][9], n;
    
    srand(time(0));
    
    for (int i=0; i<9; i++)
    {
        for (int j=0; j<9; j++)
        {
            n = rand() % 9;
            
            if (arr[i][j] == n)
            {
                n = rand() % 9;
                arr[i][j] = n;
            }
            
            arr[i][j] = n;
        }
    }
    
    for (int i=0; i<9; i++)
    {
        for (int j=0; j<9; j++)
        {
            cout<<" "<<arr[i][j];
        }
        
        cout<<endl<<endl;
    }
    
    system ("pause");
    return (0);
}

I want to generate unique numbers in the 9 by 9 array. The numbers should be in the range from 1 to 9. Each number should not repeat in a particular row or column. I tried to generate random numbers using rand() but I'm not getting unique numbers in the rows & columns. Please help me out to fix this problem soon...

Recommended Answers

All 9 Replies

After generating a random number inside the second for loop, you are comparing it with the current index only.

Instead you should check whether the number is in that row or column, if its not, accept it else generate a new one and repeat the above.

It's easy to arrange for numbers not to repeat in any row. It's also easy to arrange for numbers not to repeat in any column. However, at least at first thought, it is quite difficult to arrange for both conditions to hold at the same time, with the numbers still being random in any sense.

So perhaps you might explain in more detail what you're trying to do and why.

I want to make sudoku game so at the current moment I just want to generate random numbers only...

No, you want to generate numbers that are random within some fairly strong constraints. It would not surprise me to learn that meeting those constraints is as hard as solving a sudoku puzzle.

Please help me out how to do it?

I don't know how to do it. Here is why the problem is hard.

Suppose you're trying to fill a 5x5 array that meets your requirements. Suppose you've gotten this far:

5 1 2 4 3
2 4 3 1 5
1 2 4 ?

What number can you put in the ? and still meet the requirements? It obviously can't be 1, 2, or 4, as those numbers already appear in that row or column, so you must use either 3 or 5. But 3 and 5 are both in use in the last column, so once you have filled in the ?, the number after that is impossible.

Which means that the last number you successfully filled in, 4, is also impossible.

Writing a program to figure out these constraints is hard enough that I'm not willing to spend the time to do it.

Thank you all programming gurus!

Hint: You can check the validity of the board by the sum of all the numbers within a given column or row.

Hmmmm will try & let you know guys...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.