I have the following structure to randomly generate a field of integers:
GameManager::GameManager(){
srand(time(NULL));
}
void GameManager::newGame(int sizen, int sizem) {
//ETC
for(int i = 0; i < sizen; ++i){
_gameField[i].resize(sizem);
for(int j = 0; j < sizem; ++j){
_gameField[i][j] = RandomColor(difficulty);
}
}
}
int GameManager::RandomColor(Difficulty diff){
if(diff == Easy){
return rand()%4+1;
} else if(diff == Medium) {
return rand()%5+1;
}else{
return rand()%6+1;
}
}
This however, only generates the same field of numbers each time, as if the seeding didn't work. If I put the seeding in newGame()
, it does generate different random numbers each time. Why doesn't the seeding work in the constructor?
EDIT
See the class definition here:
class GameManager : public QObject
{
Q_OBJECT
public:
GameManager();
~GameManager();
int Size_n = 12;
int Size_m = 7;
Difficulty difficulty = Easy;
void newGame(int sizen, int sizem);
QVector<QVector<int> > _gameField;
QVector<QPair<int,int> > _taboo;
private:
int RandomColor(Difficulty diff);
};