lundi 12 novembre 2018

(not so) Randomly generated rabbits [duplicate]

This question already has an answer here:

Im having issues with a function that has to generate randomly some rabbits.

Despite what class Rabbit is, that's the relevant piece of code:

void constructRabbits (int anum) {
    for (int i = 0; i < anum; i++) {
       std::srand((unsigned)time(nullptr)); //seeding engine
       int age = std::rand() % 5;
       bool sex = std::rand() % 2;
       int is_vampire = std::rand() % 1000;

       Rabbit *rab = new Rabbit(age, sex, is0or1(is_vampire)); 
       myfarm.push_back(*rab);
       if (sex) { females++; }
       else { males++; }
       }
   }

And in main():

int main() {
constructRabbits(5);
for (farm_it = myfarm.begin(); farm_it != myfarm.end(); farm_it++) {
    std::cout << farm_it->getName() << ", " << farm_it->getAge() << "\n";

}
std::cout << males << ", " << females;}

And here is the issue: if i run this with CTRL+F5 shortcut ("normal" mode) the output will be:

Dante, 4 Dante, 4 Dante, 4 Dante, 4 Dante, 4 5, 0

Only if i run this in debug mode with a debug interruption point on the constructRabbits() function the SAME code will run fine giving me some randomly generated rabbits.

What am i missing?




Aucun commentaire:

Enregistrer un commentaire