public class TestRandom { public static void main(final String[] args) { int array[] = new int[6]; int i = 0; while (i < 6) { Random r = new Random(); int temp = r.nextInt(44); boolean flg = true; for (int j = 0; j <= i; j++) { if (temp == array[j]) { flg = false; break; } } if (flg) { array[i] = temp; i++; } } Arrays.sort(array); for (int a : array) { System.out.println(a); } } }