how to use math.random in java with a range code example
Example 1: java random numbers in specific range
import java.util.Random;
Random rand = new Random();
int random_integer = rand.nextInt(upperbound-lowerbound) + lowerbound;
Example 2: random number in range java
(int)(Math.random() * ((max - min) + 1)) + min
Copy
Example 3: java generate random integer in range
import java.util.concurrent.ThreadLocalRandom;
int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);
Example 4: how to get random number in java in range
private static int getRandomNumberInRange(int min, int max)
{
if (min >= max) {
throw new IllegalArgumentException("max must be greater than min");
}
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
new Random().nextInt(5);
new Random().nextInt(6);
new Random().nextInt(7);
new Random().nextInt(8);
new Random().nextInt(9);
new Random().nextInt(10);
new Random().nextInt(11);
new Random().nextInt(5 + 1)
new Random().nextInt(6 + 1)
new Random().nextInt(7 + 1)
new Random().nextInt(8 + 1)
new Random().nextInt(9 + 1)
new Random().nextInt(10 + 1)
new Random().nextInt(11 + 1)
new Random().nextInt(5 + 1) + 10
new Random().nextInt(6 + 1) + 10
new Random().nextInt(7 + 1) + 10
new Random().nextInt(8 + 1) + 10
new Random().nextInt(9 + 1) + 10
new Random().nextInt(10 + 1) + 10
new Random().nextInt(11 + 1) + 10
new Random().nextInt((max - min) + 1) + min
new Random().nextInt((30 - 10) + 1) + 10
new Random().nextInt((20) + 1) + 10
new Random().nextInt(21) + 10
new Random().nextInt((max - min) + 1) + min
new Random().nextInt((99 - 15) + 1) + 15
new Random().nextInt((84) + 1) + 15
new Random().nextInt(85) + 15
Example 5: java random number generator in range
int rand = ThreadLocalRandom.current().nextInt(x,y);