-6

I'm writing a method in java which relates to a die throw and I try to use the math.random() but i realized that 0 is included in the random integers involved. Additionally, I don't quite get the *7 part what does it mean?

I went to research from the Java API but it doesn't mention any bit about this or is it I am doing the wrong research? Thanks so much for reading!

public int dieThrow()
{
    int num = (int)(Math.random() *7); //returns an integer 
    return num;
}
alan_ogz83
  • 73
  • 1
  • 2
  • 10

1 Answers1

3

This is a pretty simple exercise. You observe that 0 is a possible outcome, so you simply + 1 to the result, like so:

public int throwDie()
{
    return (int)(Math.random() * 6) + 1;
}
BlackBox
  • 2,223
  • 1
  • 21
  • 37