-
Notifications
You must be signed in to change notification settings - Fork 49
Open
Labels
Description
Problem
Before Java 17, java.util.Random#nextInt did not have a way to specify both a start and an end point. You could only specify the upper bound (the size of the range). To get a number between 20 and 99, you had to do the math manually:
// Calculate the range size: 100 - 20 = 80.
// Generate a random number between 0 and 79 using nextInt(80).
// Add the base offset (20) to that result.
Random random = new Random();
int randomPart = 20 + random.nextInt(80); // Returns a number from 20 to 99Description of the proposed new feature
Now, with RandomGenerator, you can use the new #nextInt(origin, bound API to do this in a more idiomatic manner:
Random random = new Random();
int randomPart = random.nextInt(20, 100); // Returns a number from 20 to 99
- Support a stylistic preference.
- Avoid a common gotcha, or potential problem.
- Improve performance.
I would like to rewrite the following code:
a + random.nextInt(b)to:
random.nextInt(a, a + b)Considerations
Participation
- I am willing to submit a pull request to implement this improvement.