for each from 1 to 6 java code example
Example 1: how to get random number in java in range
package com.mkyong.example.test;
import java.util.Random;
public class TestRandom {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(getRandomNumberInRange(5, 10));
}
}
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;
}
}
Example 2: how to generate random numbers in java within range
import java.util.Random;
Random rand = new Random();
int random_integer = rand.nextInt(upperbound-lowerbound) + lowerbound;