Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help with probability assignment

  1. #1
    Join Date
    Feb 2012
    Thanked 0 Times in 0 Posts

    Default Need help with probability assignment

    In the game of Craps, a "Pass Line" bet proceeds as follows. Using two six-sided dice, the first roll of the dice in a craps round is called the "Come Out Roll." The bet immediately wins when the come out roll is 7 or 11, and loses when the come out roll is 2, 3, or 12. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number becomes "the point." The player keeps rolling the dice until either 7 or the point is rolled. If the point is rolled first, then the player wins the bet. If the player rolls a 7 first, then the player loses.
    Write a program that plays the game of Craps using the rules stated above so that it simulates a game without human input. Instead of asking for a wager, the program should just calculate if the player would win or lose. The program should simulate rolling the two dice and calculate the sum. Add a loop so that the program plays 10,000 games. Add counters that count how many times the player wins, and how many times the player loses. At the end of the 10,000 games, compute the probability of winning, i.e. Wins / (Wins + Losses) and output this value. Over the long run, who is going to win the most games of Craps, you or the house?

    Note: To generate a random number x, where 0 <= x < 1, use x = Math.random(); . For example, multiplying by six and converting to an integer results in an integer that is from 0 to 5.

    Use a nested loop, with the outer loop for the number of games and an inner loop for the scenario of rolling the point.

    can anyone help me with this please?

    this is all i have so far.

    public static void main(String[] args) {

    Random diceRoller = new Random();
    double NUM_GAMES;
    for (NUM_GAMES = 10000)
    for (int i = 1; i <= 12; i++) {
    int roll = diceRoller.nextInt(11) + 2;


    i am so stumped. i understand what the assignment is all about. i'm supposed to create a program that would simulate a dice roll for 10,000 times but how am I supposed to do taht by using loops? The assignment says to use a nested loop but I'm not sure how. Am I supposed to define the constants using the "private static final int" for the counts, wins and losses? If someone can help me with this that would be greatly appreciated.

  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    In Hyrule enjoying a chat with Demise and Ganondorf
    My Mood
    Thanked 48 Times in 42 Posts

    Default Re: Need help with probability assignment

    Okay. So think about this logically:

    Your outer loop needs to repeat 10000 times. Right now you have...

    for (NUM_GAMES = 10000)

    ...but this won't work. All you're doing is telling the for loop to set the value of NUM_GAMES to 10000; you're not actually testing anything. The syntax for a for-loop is

    for(VariableDeclaration; TestExpression; Increment)

    Right now, you have the declaration (NUM_GAMES = 10000), but think about this: do you start at the 10000th game, or the 1st game? Then, for the test: you want the loop to continue as long as NUM_GAMES is less than or equal to 10000. Finally, the increment (easy): every time a game is completed, you add one to the current value of NUM_GAMES.

    Next, for your random:

    int roll = diceRoller.nextInt(11) + 2;

    This is confusing, but think about it: if you roll two dice, what is the chance of getting, say, a 6 compared to the chances of getting a 12? The 6 is more likely because there are more ways to roll a 6 (2 + 4, 3 + 3, etc.), whereas there's only one way to roll a 12 (6 + 6). However, using only one random from 0 to 12 doesn't accurately represent this situation. Instead of how you have it now, I'd do as the instructions suggested:

    Quote Originally Posted by The Instructions
    To generate a random number x, where 0 <= x < 1, use x = Math.random(); . For example, multiplying by six and converting to an integer results in an integer that is from 0 to 5.
    Use two randoms that range from 1 to 6, then add them to get your total (so instead of just declaring one int -- roll -- I'd do rollA, rollB, and sum).

    Now, note that you will need two more integers; one to hold the number of wins and another to hold the number of losses.

    Hm... I think I'll stop there for now. Try implementing those suggestions, and I'll provide more when you figure that much out. Good luck!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!

    Wanna hear something funny?

    Me too.

Similar Threads

  1. Looking for help with assignment
    By n00bprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2011, 05:38 PM
  2. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  3. random uniform distribution with probability assignment
    By blascobz in forum Object Oriented Programming
    Replies: 0
    Last Post: February 28th, 2011, 09:16 AM
  4. Finding frequency and probability of characters in a string
    By Aberforth in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 31st, 2010, 02:02 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM