Hi,
I would like to understand the mechanism of the srand(..) random number generator.
In the enclosed a small example. I would like to place 'X' character into all the position of the given 3x3 matrix randomly. If the selected position is occupied, the program will have to find a new empty position ( using the random generator )
The random genenator works well till the 5th-6th position, but after that it couldn't find new empty place.
Code:
#include<stdio.h>
#include<time.h>
#include <stdlib.h>
int main()
{
int row = 0,column = 0;
int i;
char table[3][3]= { {'?','?','?'}, {'?','?','?'}, {'?','?','?'} };
int x=0;
while (x<9)
{
do
{
srand(time(NULL) +100);
row = rand() % 3;
srand(time(NULL));
column = rand() % 3;
printf("row: %d, column: %d\n", row, column);
}
while ( table[row][column]!='?' );
table[row][column]='X';
int j,k;
for (j=0; j<3;j++)
{
for(k=0;k<3;k++)
{
printf("%c\t", table[j][k]);
}
printf("\n");
}
printf("\n\n");
getchar(); // demonstrate the table elements
x++;
}
}
I noticed, the random values didn't change so quickly. ( you can see it on the console if you run the code ). What would be the background of the srand() ? Do you have any idea how to refine the parameters in srand() fuction? Maybe, Shall I try it on a better processor?
Very thx for your help!