Java Stream How to - Generate array with 6 random integers








Question

We would like to know how to generate array with 6 random integers.

Answer

import java.util.Arrays;
import java.util.Random;
/*from   ww w.  ja  v a  2  s . co  m*/
public class Main {
  public static void main(String[] args) throws Exception {
    int[] ints = new Random().ints(1, 50).distinct().limit(6).toArray();
    System.out.println(Arrays.toString(ints));
  }

}

The code above generates the following result.