mercredi 12 février 2020

Is there a way to produce random numbers simultaneously in c++? If not, is there a work around?

class Class{
  int produceRandom(){
    int ranNum = rand() % 5;
    //other unrelated code
    return ranNum;
}

std::vector<Class> classes;

int main(){
  srand(time(NULL));
  //codey code, unrelated

  if (classes.empty() == false){
    for (int i; i < classes.size(); i++){
      //Code produces a number of classes if certain conditions are met
      int useRanNum = classes[i].produceRandom();
    }
  }
}

The numbers are random enough for each iteration through the code, but each classes produces the same number for each iteration.

I've hit a complete block here and it's driving me insane. Is their a work around? Am I doing something wrong? I've tried using a Mersenne Twister, but to the same result. Any help is greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire