import random java code example
Example 1: java random number
import java.util.Random;
Random rand = new Random();
int n = rand.nextInt(50);
n += 1;
Example 2: how to create a random number in java
import java.util.Random;
class scratch{
public static void main(String[] args) {
Random rand = new Random();
System.out.println( rand.nextInt(5) );
}
}
Example 3: random java
import java.util.Random();
Random <name> = new Random();
<variable> = <name>.nextInt(<excuslive top limit>);
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);