-1

/*I'm sorry I'm not in the English-speaking world. Please understand. *My question is why line★ doesn't work?? *When it's constant instead of '(double)rand() / RAND_MAX', it works! */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
    int cash = 50;
    int bets = 0;
    srand((unsigned)time(NULL));


    for (int i = 1; i <= 1000; i++) {
        while (cash > 0 && cash < 250) {
            if ((double)rand() / RAND_MAX < 0.5) ★
                cash++;
            else cash--;
        }
        if (cash == 250)
            bets++;
    }

    printf("initial amount $50\n");
    printf("target amount $250\n");
    printf("%d wins out of 1000\n", bets);
    printf("odds of winning=%lf", bets / 1000);

    return 0;
}
  • 9
    What does "doesn't work" mean? – Retired Ninja Oct 13 '22 at 09:31
  • 2
    Have you tried stepping through the code statement by statement in a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems), while monitoring variables and their values? For this it also helps if you break up complex statements and expressions into smaller and simpler parts. For example, store the result of `rand()` in a (floating point) variable. Then divide that variable with `RAND_MAX` and store in another variable. This way it's easy to see the actual values in the debugger. – Some programmer dude Oct 13 '22 at 09:33
  • 1
    By the way, `bets / 1000` is an integer division with an integer result. You probably want `bets / 1000.0` to get a floating point result. – Some programmer dude Oct 13 '22 at 09:34
  • Perhaps try a simpler test to see if the problem is `rand()` or the rest of the logic. `rand()` seems to work for me: https://godbolt.org/z/h59ExqM67 – Retired Ninja Oct 13 '22 at 09:37
  • 1
    A side note: if you just want to increase cash 1/2 of the times, and decrease 1/2 of the times, you can replace `((double)rand() / RAND_MAX < 0.5)` with this simpler condition: `(rand() % 2 == 0)`. – wohlstad Oct 13 '22 at 09:38
  • 3
    You have a 50% change to increase or decrease `cash`. It might take a while until you get out of that loop. – Gerhardh Oct 13 '22 at 09:40
  • 1
    indeed `cash` might dither around for ever, up or down by `1`. – Weather Vane Oct 13 '22 at 09:43
  • I did some test counting number of iterations. During those 1000 runs it took between 500 and 60,000 iterations before the loop was left. Theoretically it could take forever. And I would bet it is more likely to leave because `cash==0` than `cash==250` – Gerhardh Oct 13 '22 at 09:55
  • Note: You do not reset `cash` after you once left the loop. The remaining loop will not be executed at all. – Gerhardh Oct 13 '22 at 09:56

1 Answers1

0

I've tried to test your code; the only bug I found is that you don't refill the cash variable.

Since the while loop exits either on cash = 0 or cash=250, you'll get that in the next loop the while statements exit at the first iteration.

For this reason, you will have a 100% win or 0% win.

Try to set cash = 50 before the while loop.

I give a suggestion: Do not do your calculus inside the if statement, use something like this:

res = (double)rand() / RAND_MAX;
if (res < 0.5) {
...
}

in this way is easier to see through the debugger what value is res.

I hope this can help you.

TKingu
  • 26
  • 5