0

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;
    }
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • 2
    Just call `srand` once at the start of your program or even better use the c++11 random library which is much higher quality – Alan Birtles Dec 01 '20 at 07:21
  • A fairly dumb fix is to change `srand(time(0));` to `srand(time(0)^rand());` – David Schwartz Dec 01 '20 at 07:39
  • [srand() — why call it only once?](https://stackoverflow.com/q/7343833/995714). But you don't use srand in C++. Use [`std::random_device `](https://stackoverflow.com/q/13445688/995714) instead – phuclv Dec 01 '20 at 11:41

0 Answers0