lundi 30 novembre 2020

How to use srand() in a class function and yield different results [duplicate]

I am trying to create a class in c++ where the class functions calculate a chance of something happening. But when I try to implement the functions using srand(time(0)) it just produces the same outcome every time. mooseChance is a private variable of the class Hunting. How do implement srand(time(0)) to get random odds in the function call?

    void Hunting::calcMooseChance()
    {
        srand(time(0));
        bool mooseAppear = false;
        mooseAppear = (rand() % 100) < 50;
        mooseChance = mooseAppear;
    }



Aucun commentaire:

Enregistrer un commentaire