this post was submitted on 01 May 2025
5 points (100.0% liked)

Learn Programming

1819 readers
1 users here now

Posting Etiquette

  1. Ask the main part of your question in the title. This should be concise but informative.

  2. Provide everything up front. Don't make people fish for more details in the comments. Provide background information and examples.

  3. Be present for follow up questions. Don't ask for help and run away. Stick around to answer questions and provide more details.

  4. Ask about the problem you're trying to solve. Don't focus too much on debugging your exact solution, as you may be going down the wrong path. Include as much information as you can about what you ultimately are trying to achieve. See more on this here: https://xyproblem.info/

Icon base by Delapouite under CC BY 3.0 with modifications to add a gradient

founded 2 years ago
MODERATORS
 

I'm going through the Programming With a Purpose course on Coursera, and trying to come up with my own implementation of some of the example programs before looking at the example. I just finished the example of a gambling situation. I was hoping to get some more eyes on my code and be told whether my version is going to behave differently from the example. Due to the nature of simulations, I can't just compare the output of the two, since it will vary.

This is the explanation of what the code is meant to represent:

Gambler (PROGRAM 1.3.8) is a simulation that can help answer these ques tions. It does a sequence of trials, using Math.random() to simulate the sequence of bets, continuing until the gambler is broke or the goal is reached, and keeping track of the number of wins and the number of bets.

Mine:

public class Gambler
{
    public static void main(String[] args)
    {
        int stake = Integer.parseInt(args[0]);
        int initialStake = stake;
        int goal = Integer.parseInt(args[1]);
        int desiredGain = goal - stake;
        int trials = Integer.parseInt(args[2]);
        int games = 0;
        int wins = 0;
        int bets = 0;

        while (games < trials)
        {
            bets++;
            if (Math.random() >= 0.5)
            {
                stake++;
                desiredGain = goal - stake;
                if (desiredGain <= 0)
                {
                    wins++;
                    games++;
                    stake = initialStake;
                }
            }
            else
            {
                stake--;
                if (stake < 1)
                {
                    games++;
                    stake = initialStake;
                }
            }
        }
        int averageBets = bets / trials;
        int percentWins = (100*wins / trials);
        System.out.println("Theoretical chance of winning: " + 100*initialStake/goal);
        System.out.println("Expected Bets: " + initialStake*(goal - initialStake));
        System.out.println(percentWins + "% wins");
        System.out.println("Average # bets: " + averageBets);
    }
}

Example:

public class Gambler2
{
    public static void main(String[] args)
    {
        int stake = Integer.parseInt(args[0]);
        int goal = Integer.parseInt(args[1]);
        int T = Integer.parseInt(args[2]);
        int bets = 0;
        int wins = 0;

        for (int t=0; t<T; t++)
        {
            int cash = stake;
            while (cash > 0 && cash < goal)
            {
                bets++;
                if (Math.random() < 0.5) cash++;
                else                     cash--;
            }
            if (cash == goal) wins++;
        }
    int averageBets = bets / T;
    int percentWins = (100*wins / T);
    System.out.println("Theoretical chance of winning: " + 100*stake/goal);
    System.out.println("Expected Bets: " + stake*(goal - stake));
    System.out.println(percentWins + "% wins");
    System.out.println("Average # bets: " + averageBets);
    }
}

The example is obviously much cleaner, but here is why I think mine should work the same:

  1. Each time the player's current stake exceeds the goal or reaches 0, the number of games played is incremented. When games == trials, the loop ends.
  2. When a game ends, stake is reset to its initial value for the next trial
  3. Bets is incremented each time the loop runs
  4. If the current stake meets or exceeds the goal, wins is incremented

I set up my conditions for interpreting the output of Math.random() opposite of the example. If that even makes a difference at all, it seems like it would take a lot more than 1000 trials before it became apparent.

I just want to make sure I'm not missing something.

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 1 points 2 days ago* (last edited 1 day ago) (1 children)

Can you maybe post the description of what it's supposed to do?

I'd need to look at it some more to be able to really tell whether it's functionally equivalent otherwise, but one thing I've spotted is that the solution code only counts it as a win when cash == goal, whereas you've assumed it to be a win when cash exceeds the goal.

[โ€“] Hammerheart 1 points 1 day ago

I added a description to OP.

Gambler (PROGRAM 1.3.8) is a simulation that can help answer these questions. It does a sequence of trials, using Math.random() to simulate the sequence of bets, continuing until the gambler is broke or the goal is reached, and keeping track of the number of wins and the number of bets.