I have a c++ program that uses a random generator to create a cube. To create the cube I have this:
void generateSquare() {
int num = 0;
srand(time(NULL));
for (int r = 0; r < MSQUARE; r++) {
for (int c = 0; c < MSQUARE; c++) {
do {
num = (rand() % (MSQUARE * MSQUARE)) + 1;
} while (!checkUnique(num));
cube[r][c] = num;
}
}
}
It only allows a number to be generated one. This function is then wrapped in a do..while loop so that if the square doesn't meet a specific condition a new square will be generated until the one that does meet the condition is generated.
The problem is that it continually is generating the same square over and over again. I thought that the srand(time(NULL)) would seed the rand() function so that it would generate different random numbers each time to create a new unique square every time it is called but it looks like that is not happening.
void checkSquare() {
do {
generateSquare();
} while (!perfect);
}
This is not the actual do...while loop but gives you an idea of how it's being called.
How do I make sure the seed is unique each time?
Aucun commentaire:
Enregistrer un commentaire