1 answer
(click or scroll down)
To generate a random integer between `min` and `max`, inclusive, you can use the following formula:
```java
int num = min + (int)(rn * (max - min + 1));
```
Hereโs how it works:
1. `Math.random()` generates a double between 0.0 (inclusive) and 1.0 (exclusive).
2. Multiplying it by `(max - min + 1)` scales it to be in the range of 0 to `(max - min + 1)` (exclusive).
3. When you cast this result to an integer, it effectively gives you a value in the range `[0, max - min]`.
4. Finally, adding `min` shifts this range up to `[min, max]`.
So the complete line to replace the `/* missing code */` would be:
```java
int num = min + (int)(rn * (max - min + 1));
```
This ensures that `num` is uniformly distributed over the integers from `min` to `max`, including both endpoints.