How to generate a random integer in a given range in Java?
Hi everyone! I'm new to Java and writing a small dice roll simulator program. I need the program to output a random number from 1 to 6 inclusive. I know about the `Random` class and the `nextInt()` method, but it seems I'm doing something wrong.
Here's my code:
```java
import java.util.Random;
public class DiceSimulator {
public static void main(String[] args) {
Random rand = new Random();
int diceRoll = rand.nextInt(6); // I thought this would give from 1 to 6
System.out.println("Rolled: " + diceRoll);
}
}
```
The problem is that the program sometimes outputs 0, but I need the minimum value to be 1. I read the documentation, but it says something like "nextInt(int bound) returns from 0 to bound-1". So with bound = 6 I get numbers 0,1,2,3,4,5 —