java print 5 random numbers code example
Example 1: 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 2: random number generator java
import java.util.Random;
class GenerateRandom {
public static void main( String args[] ) {
Random rand = new Random();
int upperbound = 25;
int int_random = rand.nextInt(upperbound);
double double_random=rand.nextDouble();
float float_random=rand.nextFloat();
System.out.println("Random integer value from 0 to" + (upperbound-1) + " : "+ int_random);
System.out.println("Random float value between 0.0 and 1.0 : "+float_random);
System.out.println("Random double value between 0.0 and 1.0 : "+double_random);
}
}