The following shows how you can get unique random numbers in Java without using collection classes. The principle is to create an array of the possible choices, shuffle it and then select n numbers from the front. The application below will choose your six lottery numbers if run without arguments. The code can be downloaded HERE.
import java.util.Arrays;
public class RandomArrayWithoutDuplicates {
// Look at the javadoc for the principal method to see what three arguments can be passed into main
public static void main(String[] args) {
int[] arrayOfRandoms = null;
int numberOfRandoms = 0;
int includedLowerBound = 0;
int includedUpperBound = 0;
if (args.length < 1) {
numberOfRandoms = 6;
includedLowerBound = 1;
includedUpperBound = 49;
} else if (args.length == 3) {
numberOfRandoms = Integer.parseInt(args[0]);
includedLowerBound = Integer.parseInt(args[1]);
includedUpperBound = Integer.parseInt(args[2]);
}
arrayOfRandoms = RandomArrayWithoutDuplicates.nRandomsFromRange(numberOfRandoms,
includedLowerBound, includedUpperBound);
System.out.printf("You chose the following array of length %d of random(s) between included lower bound %d and included upper bound %d%n",
numberOfRandoms, includedLowerBound, includedUpperBound);
System.out.println(Arrays.toString(arrayOfRandoms));
}
/**
*
* Get an array of n random numbers from a range
*
* @param n How many numbers to choose
* @param min The (included) lower bound of the range
* @param max The (included) upper bound of the range
*
* @return An array of n values
*/
public static int[] nRandomsFromRange(int n, int min, int max) {
int pSize = max - min + 1;
if (n > pSize) {
throw new IllegalArgumentException(String.format(
"Number of choices (%d) cannot be greater than size of range (%d)",
n, pSize));
}
int[] nChoices = new int[n];
int lo = min;
int[] possibilities = new int[pSize];
for (int i = 0; i < pSize; i++) {
possibilities[i] = lo++;
}
// Shuffle possibilities array
for (int i = pSize; --i >= 1;) {
// Swap
int j = (int) (Math.random() * (i + 1));
int tmp = possibilities[i];
possibilities[i] = possibilities[j];
possibilities[j] = tmp;
}
// Get the choices from the front of the shuffled array
for (int i = 0; i < n; i++) {
nChoices[i] = possibilities[i];
}
return nChoices;
}
}
public class RandomArrayWithoutDuplicates {
// Look at the javadoc for the principal method to see what three arguments can be passed into main
public static void main(String[] args) {
int[] arrayOfRandoms = null;
int numberOfRandoms = 0;
int includedLowerBound = 0;
int includedUpperBound = 0;
if (args.length < 1) {
numberOfRandoms = 6;
includedLowerBound = 1;
includedUpperBound = 49;
} else if (args.length == 3) {
numberOfRandoms = Integer.parseInt(args[0]);
includedLowerBound = Integer.parseInt(args[1]);
includedUpperBound = Integer.parseInt(args[2]);
}
arrayOfRandoms = RandomArrayWithoutDuplicates.nRandomsFromRange(numberOfRandoms,
includedLowerBound, includedUpperBound);
System.out.printf("You chose the following array of length %d of random(s) between included lower bound %d and included upper bound %d%n",
numberOfRandoms, includedLowerBound, includedUpperBound);
System.out.println(Arrays.toString(arrayOfRandoms));
}
/**
*
* Get an array of n random numbers from a range
*
* @param n How many numbers to choose
* @param min The (included) lower bound of the range
* @param max The (included) upper bound of the range
*
* @return An array of n values
*/
public static int[] nRandomsFromRange(int n, int min, int max) {
int pSize = max - min + 1;
if (n > pSize) {
throw new IllegalArgumentException(String.format(
"Number of choices (%d) cannot be greater than size of range (%d)",
n, pSize));
}
int[] nChoices = new int[n];
int lo = min;
int[] possibilities = new int[pSize];
for (int i = 0; i < pSize; i++) {
possibilities[i] = lo++;
}
// Shuffle possibilities array
for (int i = pSize; --i >= 1;) {
// Swap
int j = (int) (Math.random() * (i + 1));
int tmp = possibilities[i];
possibilities[i] = possibilities[j];
possibilities[j] = tmp;
}
// Get the choices from the front of the shuffled array
for (int i = 0; i < n; i++) {
nChoices[i] = possibilities[i];
}
return nChoices;
}
}