0

I get 0 and 5000 randomly (which is how I wanted it to work, a 30% chance to receive 5000) however I also get 500 which I assume is due to the fact that it's not running the function at all? I'm not really sure, I'm very new to coding.

int gamble2 (int money){
    srand(time(0));
    int rand();
    if (rand() % 100 >= 70){
        money += 4500;
    }
    else if (rand() % 100 < 70){
        money -= 500;
    }
    return money;
}

main()
{
    int startingmoney = 500;
    int money = startingmoney;
    int objective = 100000;
    cout << gamble2(money) << endl;
}
  • 2
    What do you think the line `int rand();` is doing? That should've thrown some kind of warning or error. You should always compile with all warnings turned on and pay attention to them. – Random Davis Mar 09 '22 at 19:02
  • 4
    You are rolling *twice* in your function. First result you check if it's greater than 70, second if it's less. You should save result of `rand()` call in a variable and check that variable or remove `else if` and replace it with `else`. – Yksisarvinen Mar 09 '22 at 19:03
  • 3
    Unrelated, `srand(time(0));` does *not* belong in that function if it is *ever* to be invoked more than once. It should be invoked near/at the top of `main` and never done again for the life of the process. – WhozCraig Mar 09 '22 at 19:04
  • That's... not exactly a right duplicate. It's a part of the problem, but not the main one. – Yksisarvinen Mar 09 '22 at 19:07
  • @Yksisarvinen do you believe it's actually worth reopening? – πάντα ῥεῖ Mar 09 '22 at 20:11

0 Answers0