Java Data Type How to - Generated random integers within a range


We would like to know how to generated random integers within a range.


import java.util.Arrays;
/*from w  w  w . j av a2 s  . c o  m*/
public class Main {
  public static void main(String[] args) {
    int[] randomArray = randomArray(5);
  //generate random integer within a range
  public static int randomInt(int low, int high) {
    return (int) (Math.random() * (high - low)) + low;

  public static int[] randomArray(int n) {
    int[] a = new int[n];
    for (int i = 0; i < a.length; i++) {
      a[i] = randomInt(0, 100);
    return a;

The code above generates the following result.