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.

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.

February 13th, 2012, 03:23 PM

snowguy13

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!