1

I am trying to write a method that simulates a dice game in which a dice (with values from 1 to 6) is rolled four times, and that returns true if there is at least one 6 rolled, and false otherwise. The game is 'won' if true is returned.

I declare two variables to keep track of the number of times a '6' is thrown, and another to keep track of whether the game is won (i.e. if a 6 is thrown).

I then use a for loop to simulate the rolling of the dice; this increments the count of the number of 6's rolled, if a 6 is thrown.

I then use conditionals to return true, if a 6 has been thrown, and false otherwise.

I would expect that if I run the code enough times, that true would be returned on at least some occasions (i.e. I would 'win' the game). When I actually run the code however, I only ever get false returned.

What am I doing wrong?

Here is my code:

     import java.util.Random;

     public class DiceGame {
Random generator;

public DiceGame() {   
    generator = new Random(45);
}

/** 
 * Throw a die four times and bet on at least one 6. 
 * @return true if the chevalier wins. 
 */
public boolean game1()  {
    int trueDice = 0; 
    boolean gameWon = false; 

    for (int i=0; i < 4;i++)  {
    int dieRoll = generator.nextInt(6);
    if (dieRoll == 6)  {
        trueDice++;
        }
      }

    if (trueDice >= 1)  {
            gameWon = true;
       } else {
           gameWon = false;
        }
    return gameWon; 
}

Thanks in advance for any help.

user3208200
  • 89
  • 1
  • 4

2 Answers2

3

nextInt gives you a number between 0 (inclusive) and and the specified value (exclusive). So currently you are generating random numbers in the range of 0-5. Change it to:

int dieRoll = generator.nextInt(6) + 1;

and you will get numbers from 1-6.

Mike de Dood
  • 393
  • 1
  • 10
1

Seed your Random generator like following:

generator = new Random(System.currentTimeMillis());

Not any issue with 45, but time seed is a better option to remove predictability in your program.

Then for the line: int dieRoll = generator.nextInt(6);, it should be:

int dieRoll = 1 + generator.nextInt(6);
Aman Agnihotri
  • 2,973
  • 1
  • 18
  • 22
  • 1
    The no-arg constructor will seed the Random with `a value very likely to be distinct from any other invocation of this constructor.` – Greg Kopff Feb 27 '14 at 12:57
  • 1
    Yeah that as well, and the OP used a value of `45` to initialize his code, making each run of the program very predictable. By the way, the no args constructor uses `System.nanoTime()` call to seed itself. – Aman Agnihotri Feb 27 '14 at 13:09
  • Since using `currentTimeMillis` is worse than omitting the argument, what is the point of including it? – Marko Topolnik Feb 27 '14 at 13:18
  • Technically `System.currentTimeMillis()` is much faster than `System.nanoTime()`. Don't believe me? Well read this then: [NanoTime slower than CurrentTimeMillis](http://stackoverflow.com/questions/19052316/why-is-system-nanotime-way-slower-in-performance-than-system-currenttimemill) and also [Drift between CurrentTimeMillis and NanoTime](http://stackoverflow.com/questions/5839152/why-do-system-nanotime-and-system-currenttimemillis-drift-apart-so-rapidly). That's why I prefer `System.currentTimeMillis()`. An added benefit is that it is guaranteed to never repeat the same value. – Aman Agnihotri Feb 27 '14 at 13:28