How to make Sudoku board like number generator with some catch:
- In one line of row/column there are random non-repeating numbers from 1 to 9 ?
Here are my source code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int matrix [9][3][3];
srand(time(NULL));
mengisi_Matrix(matrix);
tampilkan_Matrix(matrix);
return 0;
}
void mengisi_Matrix(int matrix [9][3][3])
{
for (int i = 1; i < 10; i++)
{
for (int j = 1; j < 4; j++)
{
for (int k = 1; k < 4; k++)
{
matrix[i][j][k] = (rand() % 9) + 1;
}
}
}
}
void tampilkan_Matrix(int matrix [9][3][3])
{
for (int i = 1; i < 10; i++)
{
printf("| ");
for (int j = 1; j < 4; j++)
{
for (int k = 1; k < 4; k++)
{
printf("%d ", matrix[i][j][k]);
}
printf("| ");
}
printf("\n");
if (i % 3 == 0)
{
printf("- - - - + - - - + - - - - \n");
}
}
}
I tried to fill up in one line via one by one random numbers using srand().
But before input it into let say array[1][1][2], I want to compare the random generated number with same all of the array in same row/column, if there is already exist the number it will generate new random number and so on and so on until all of array[9][3][3] filled up.
I have tried bunch of my own logic and tried to ask AI but no still no solution, there is always unwanted and non-logic (for me) problems/errors/warning such as implicit declaration or conflicting types. Hope you guys who are more pro and expert than me can help me to figure this out.
You can use a Fisher–Yates shuffle.
For example,
Whenever you pick a number for a position, every available option has an equal change of being picked. This means this is a fair algorithm (given a fair implementation of
rand_int).