• Post Reply Bookmark Topic Watch Topic
  • New Topic

Randon Dice Roll 10,000 Times: 7 and 11 winners every other number lose  RSS feed

 
Rebecca Berry
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again all! So I have to write a program that randomly rolls a set of dice 10,000 times.  If the roll produces a total of 7 or 11 it is considered a win. Any other number (2, 3, 4, 5, 6, 8, 9, 10, and 12) is considered a loss. The output of the program should state the total wins and total losses as well as a probability. The problem I am having is that my code appears to be producing wins only.  Here is my code so far:


 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
edit - didn't quite see your "The problem I'm having..." sentence when I first composed my reply. But all the advice still holds.

well...you didn't actually ask a question, so it's hard to know what you are looking for.

Generally, though...

you don't want to put all your code in the main method.  You should consider making many smaller methods that each do one thing.  For example, a method that generates the value of a single die roll.  Then you can make a method that generates the value of two rolls added together - and it should make use of that first method (probably twice). 

When testing/writing your code you may want to consider making it only roll ten times, instead of 10k times. You can also use System.out.println to write stuff out and see what's going on.

You are only rolling the dice once each. So on lines 10-12, you roll the dice once and get the total. Then you use that value (which never changes) 10,000 times.  You probably need to put lines 10-12 inside your for-loop (assuming you don't do what I suggest above.

Finally, Why do you have an "else if" condition?  You know the values can only be 2-12.  If you test for 7 and 11, and the roll is not that, then a) it HAS to be one of the other nine values, but also, it MUST be a losing roll...i think you can just have an "else".
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue stems from the fact that you are only rolling the die once. The die rolling happens once out of the for loop. Like Fred said, you want to create a method to roll the die then call that method within the for loop. Hope that helps.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!