435,583 Members | 3,411 Online
Need help? Post your question and get tips & solutions from a community of 435,583 IT Pros & Developers. It's quick & easy.

# Limiting duplicate entries in a matrix. How to make it look like a sudoku?

 P: 2 Hello. I have made a square matrix of order nxn with random values in it. The entries are 1 to n appearing randomly as desired. Now i would like to limit all entries to appear strictly n times. for e.g. in the code below the output is a 9x9 matrix with entries 1 to 9. I want every number to appear only 9 times in the matrix to make it look like a sudoku. So how to do that? Expand|Select|Wrap|Line Numbers #include  #include  #include    using namespace std;   const int ROWS = 9; const int COLS = 9;   //no more magic numbers. Named type now typedef int Matrix[ROWS][COLS];   int getrandomval();   void fillrandom(Matrix mat) {       for (int row = 0; row < ROWS; row++)     {         for (int col = 0; col < COLS; col++)         {             mat[row][col] = getrandomval();         }     } }   int getrandomval() {     int x = 0;     x = rand() % 9 + 1;     return x; }   void show(Matrix m) {     for (int row = 0; row < ROWS; row++)     {         for (int col = 0; col < COLS; col++)         {             cout << " " << m[row][col] << " ";         }         cout << endl;     }     cout << endl; }   int main() {     srand(time(0));       //using our constant     cout << "\nGenerating the random number matrix of order" << ROWS << " x " << COLS << "...\n\n\n";       Matrix mat; //declaring the variable     getrandomval();     fillrandom(mat); //filling our variable     show(mat); //showing whats in the matrix     return 0; }   Oct 3 '16 #1

NewCplusplus,

Here is a potential algorithm for selecting random numbers to meet your criteria:
1) create an array of all the possible numbers (digits).
1.1) call this array "numberCount"
1.2) numberCount.length = 10 for numbers zero through 9.
2) zero all entries in "numberCount".
3) decide how many of each number will be placed into the
matrix
3.1) assign "numberCount[number]" to the "how many" count.
4) compute totalCount as the sum of all numberCount entries.
4.1) totalCount = numberCount[0]+numberCount[1]+...
5) pick a number from zero to (totalCount-1)
5.1) valueCount
6) span the "numberCount" array until the running total of values exceeds valueCount
6.1) keep the index of interest.
6.2) decrement the "numberCount" element that was selected.
7) return index of interest.

In C++, this is my first cut at the algorithm.
Please note that you are responsible for populating the numberCount array before using this method.
Expand|Select|Wrap|Line Numbers
1. //--histogram of remaining numbers to place
2. //--this is the initial state, user must populate
3. int numberCount[10] = {0,0,0,0,0,0,0,0,0,0};
4.
5. // fetch a remaining numbers
7. int getRandomValue()
8. {
9.   //--remaining number of numbers to place
10.   int totalCount = 0;
11.   for (auto count : numberCount)
12.     totalCount += count;
13.
14.   //--which number do we pick
15.   int valueCount = rand() % totalCount
16.
17.   //--which number was selected?
18.   //--march across array from zero to nine.
19.   int value;
20.   int incrementCount = 0;
21.   bool spin = true;
22.   for (int index; (spin  and  (10 > index)); index++)
23.   {
24.     incrementCount += numberCount[index];
25.     if (incrementCount > valueCount)
26.     { // found!
27.       value= index;
28.       numberCount[index]--;
29.       spin = false;
30.     }
31.   }
32.
33.   //--done
34.   return value;
35. }
Good luck; I do hope that you create a new, fun game for us!
Oralloy