Question:
How do you generate random integers in a range with Java?
Code:
import java.util.Random; public class RandomNumbers { public static void main(String[] args) { Random r = new Random(); int x; // generate random number between 10 and 20 int low=10; int high=20; x = r.nextInt(high-low)+low; System.out.println(x); } }
Output:
$ java RandomNumbers 15