Java Random generate different random number

Description

Java Random generate different random number


import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class Main {
  public static void main(final String[] args) throws Exception {
    Random random = new Random();
    Set<Integer> intSet = new HashSet<>();
    while (intSet.size() < 10) {
      intSet.add(random.nextInt(100) + 1);
    }/*  w  w  w  . java2  s .c om*/
    Integer[] ints = intSet.toArray(new Integer[intSet.size()]);
    System.out.println(Arrays.toString(ints));
  }
}



PreviousNext

Related