mercredi 13 novembre 2019

Srand Function Producing Same Result At Every Call

I have this function that should represent a coin flip

bool SkipList::alsoHigher() const
{
    srand(time(nullptr));
    int random = 1 + rand() % 2;
    return random == 1;
}

It is used for the condition within a while-loop via:

bool goHigher = alsoHigher();
while (goHigher)
{
   // some code
   goHigher = alsoHigher();
}

But each time my program is ran, this function produces the same result for all calls within that program run through. The function will return true for all instances on one program run and on another it will return false for all instances, whereas it should return true and false differently per while loop iteration. Is there something wrong with how I'm trying to use alsoHigher() or does this look fine and my problem is elsewhere?




Aucun commentaire:

Enregistrer un commentaire