435,583 Members | 3,411 Online + Ask a Question
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

✓ answered by Oralloy

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+numberCount+...
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 = {0,0,0,0,0,0,0,0,0,0};
4.
5. // fetch a remaining numbers
6. // updates numberCount[].
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

3 Replies

 Expert Mod 5K+ P: 9,197 Can you not put digits 1-9 in each 9 digit row of the matrix? After 9 rows each number will appear 9 times. Oct 3 '16 #2

 P: 2 That way in case of a larger size matrix (say 100) i will have to keep on punching in values and the coding would be lengthy and repetitive. This way i can substitute any value for rows and cols and get my matrix already filled with random entries. Now i just have to limit the appearance of the entries according to the order of the matrix. Once i have the matrix as desired then i can start using mathematical operations in them. Just an idea for a fun game, although i am a beginner in programming but i kept on trying and got this far. Oct 3 '16 #3

 Expert 100+ P: 983 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+numberCount+... 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 //--histogram of remaining numbers to place //--this is the initial state, user must populate int numberCount = {0,0,0,0,0,0,0,0,0,0};   // fetch a remaining numbers // updates numberCount[]. int getRandomValue() {   //--remaining number of numbers to place   int totalCount = 0;   for (auto count : numberCount)     totalCount += count;     //--which number do we pick   int valueCount = rand() % totalCount     //--which number was selected?   //--march across array from zero to nine.   int value;   int incrementCount = 0;   bool spin = true;   for (int index; (spin  and  (10 > index)); index++)   {     incrementCount += numberCount[index];     if (incrementCount > valueCount)     { // found!       value= index;       numberCount[index]--;       spin = false;     }   }     //--done   return value; } Good luck; I do hope that you create a new, fun game for us! Oralloy Oct 3 '16 #4 