5

I have this method that generates a number (1-10) for each array value listed in this method. I want the whole set of numbers to be displayed as a set of unique numbers. How to do this?

public static int generateNumbers(int[] lotteryNumbers) {

    Random randNum = new Random();

    lotteryNumbers[0] = randNum.nextInt(10);
    lotteryNumbers[1] = randNum.nextInt(10);
    lotteryNumbers[2] = randNum.nextInt(10);
    lotteryNumbers[3] = randNum.nextInt(10);
    lotteryNumbers[4] = randNum.nextInt(10);

    return lotteryNumbers[4];
}
9
  • You should not use random. Commented Sep 6, 2015 at 12:22
  • 2
    Duplicate of stackoverflow.com/questions/8115722/… Commented Sep 6, 2015 at 12:23
  • 1
    @nikpon what's wrong with Random ? It's perfectly fine Commented Sep 6, 2015 at 12:29
  • @Dici I didn't get what you asking. If you have a question, ask it in the section of questions. Commented Sep 6, 2015 at 12:37
  • @nikpon you told him not to use Random, but there is nothing wrong with this class Commented Sep 6, 2015 at 12:38

4 Answers 4

5

An easy solution is to generate a list of the 10 digits, shuffle that list and get the first five elements:

List<Integer> list = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
    list.add(i);
}
Collections.shuffle(list);
Integer[] lotteryNumbers = list.subList(0, 5).toArray(new Integer[10]);

Collections.shuffle(list) is an utility method that randomly permutes the given list in place.

If you are using Java 8, this can be written as:

List<Integer> list = IntStream.range(0, 10).boxed().collect(Collectors.toList());
Collections.shuffle(list);
int[] loterryNumbers = list.subList(0, 5).stream().mapToInt(i -> i).toArray();
Sign up to request clarification or add additional context in comments.

3 Comments

Thank @Tunkai. But is there a way how I can generate unique numbers using Random? cause I'm only allowed to use random since it's a school assignment.
There is no built-in way. What you can do is generate an int using Random in a loop until the number you have obtained has not been found before.
@N.Cre see my answer, it is basically the same algorithm as Collections.shuffle but without using a library method. It is good to know and also good for your assignment, but keep in mind in real-life Tunaki answer is better. I would not mind if you upvoted mine though :D
3

A naive technique is to pick randomly in the set you want to "shuffle" :

public static int[] generateNumbers(int exclusiveMaxValue) {
    List<Integer> values = new ArrayList<>(exclusiveMaxValue);
    for (int i=0 ; i<values.size() ; i++) values.add(i);

    int[] result = new int[exclusiveMaxValue];
    Random rd = new Random();
    for (int i=0 ; i<result.length ; i++) {
       result[i] = values.remove(rd.nextInt(values.size()));
    }
    return result;
}

However, List.remove is usually O(n), so the whole method is quadratic, which is very expensive. You can perform a shuffle in O(n) by simply swapping elements in place (that is what Collections.shuffle does) :

public static int[] generateNumbers(int exclusiveMaxValue) {
    int[] result = new int[exclusiveMaxValue];
    for (int i=0 ; i<result.length ; i++) result[i] = i;

    Random rd = new Random();
    for (int i=result.length - 1 ; i>=0 ; i--) {
       swap(result, i, rd.nextInt(i + 1));
    }
    return result;
}

private static swap(int[] arr, int i, int j) {
    int tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
}

Comments

1

This method generates the sequence of length N of unique numbers in range [0, N -1].

public static int[] generateNumbers(int length) {
    final int[] array = new int[length];
    for (int i = 0; i < length; ++i) {
        array[i] = i;
    }
    shuffle(array);
    return array;
}

For shuffling Fisher–Yates algorithm was used:

public static void shuffle(final int[] array) {
    final Random random = new Random();
    for (int i = array.length - 1; i > 0; --i) {
        final int randomIdx = random.nextInt(i + 1);
        final int temp = array[i];
        array[i] = array[randomIdx];
        array[randomIdx] = temp;
    }
}
  • Thanks of (Ronald Fisher and Frank Yates) the algorithm's time complexity is O(n)
  • This implementation works on arrays (with primitives) not on collections (with instances of Integer class that wraps a value of the primitive type int in an object) - it matters if array size is big enough

Comments

0

Here is an alternative way which uses a set and fills it until it grows to the size required. It generates numbersToDraw distinct random numbers in range from min to max (inclusive). It also preserves the order in which numbers were drawn (that is what LinkedHashSet is for).

private static Set<Integer> drawNumbers(int min, int max, int numbersToDraw) {
    if (max < min) {
        throw new IllegalArgumentException("Minimum must be less than maximum.");
    }
    if (max < 0 || min < 0) {
        throw new IllegalArgumentException("Both range numbers must be positive.");
    }
    final int countOfNumbers = max - min + 1;
    if (countOfNumbers < numbersToDraw) {
        throw new IllegalArgumentException("Range is not big enough.");
    }
    final Random randomizer = new SecureRandom();
    final Set<Integer> numbersDrawn = new LinkedHashSet<>();
    while (numbersDrawn.size() < numbersToDraw) {
        final int randomNumber = min + randomizer.nextInt(countOfNumbers);
        numbersDrawn.add(randomNumber);
    }
    return numbersDrawn;
}

If you do not require numbers to be unique, you can use this in Java 8:

final Random randomizer = new SecureRandom();

final List<Integer> numbersDrawn = IntStream
        .range(0, numbersToDraw)
        .mapToObj(i -> min + randomizer.nextInt(max - min + 1))
        .collect(Collectors.toList());

If you do not require numbers to be unique, BUT you want to print their distinct values (is that your original question?):

final Random randomizer = new SecureRandom();

final Set<Integer> numbersDrawn = IntStream
        .range(0, numbersToDraw)
        .mapToObj(i -> min + randomizer.nextInt(max - min + 1))
        .collect(Collectors.toSet());

And one more alternative for your concrete case:

final Set<Integer> distinctNumbers = Arrays
  .stream(lotteryNumbers)
  .distinct() // you can leave this as the set is distinct automatically
  .boxed()
  .collect(Collectors.toSet());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.