We would like to know how to generate 6 different random numbers in java.
import java.util.Arrays; import java.util.HashSet; import java.util.Random; import java.util.Set; //from w w w . j a v a2 s .co m public class Main { public static void main(final String[] args) throws Exception { Random random = new Random(); Set<Integer> intSet = new HashSet<>(); while (intSet.size() < 6) { intSet.add(random.nextInt(49) + 1); } Integer[] ints = intSet.toArray(new Integer[intSet.size()]); System.out.println(Arrays.toString(ints)); } }
The code above generates the following result.