Java Collection How to - Fill an array with specific random numbers








Question

We would like to know how to fill an array with specific random numbers.

Answer

import java.util.Arrays;
import java.util.Random;
/*  w  w  w .  j a  v a2 s . c  o  m*/
public class Main {
  public static void main(String[] args) {
    int[][] array = new int[2][10];
    Random rand = new Random();
    for (int i = 0; i < 10; i++) {
      array[0][i] = i;
      array[1][i] = (int) rand.nextInt(3) + 1;
    }

    System.out.println(Arrays.deepToString(array));

  }
}

The code above generates the following result.