1

I know this question has been answered out there, but those solutions don't fit in with the way I'm going about, so I'm enquiring to see if there is a simpler solution.

I'm using the set interface and I need there to be 6 random numbers and you can't have duplicates in the set interface.

This is what I've currently got, the way I I'm doing it is not ideal and often causes crashes.

public void drawLotto(){ //The validation I have here I know isn't the most effective way and is-
        Random r = new Random();//resource comsuning but this was the only way I could think of doing it.
        int draw[] = new int[6];
        int min = 1;
        for(int i = 0; i < draw.length; i++){
            draw[i] = r.nextInt(lotteryMax-min) + min;
            lotteryDraw.add(draw[i]);
        }
        int size = lotteryDraw.size();
        if(size != 6){
            drawLotto();
        }
        for(int i = 0; i < draw.length; i++){
            System.out.println(draw[i] + " ,");
        }
        System.out.println();
    }
``
Thank you, any help is appreciated. 
2
  • have you used a hashmap before? I would use a hashmap to store your values as you get them and as you create values with nextInt you could check your map to see if the values have already been created Commented Feb 17, 2021 at 17:54
  • r.nextInt(lotteryMax-min) + min Your pool of numbers is likely very small. Just add all the possible numbers to an array/list, then SHUFFLE it and take the first six values. This will guarantee no duplicates... Commented Feb 17, 2021 at 17:56

3 Answers 3

2

The reason you have problems is because you recursively call drawLotto(), which will in turn create a new instance of the Random. If drawLotto() cannot create a correct list, it will have to do a full retry of all 6 numbers. This might cause your application to use a high amount of memory, resulting in the crash you experience

One way you could do this is by keep looping until you find 6 unique numbers:

public void drawLotto(){
    Random r = new Random();
    Set<Integer> draw = new HashSet<>();
    int min = 1;
    int lotteryMax = 50;

    while(draw.size() < 6){
        draw.add(r.nextInt(lotteryMax-min) + min);
    }

    String lotteryDrawing = draw.stream().map(String::valueOf).collect(Collectors.joining(" ,"));

    System.out.println(lotteryDrawing);
}

Though you have to make sure that your lotteryMax is higher than the number you need

Sign up to request clarification or add additional context in comments.

Comments

2

check this out

public void drawLotto(){
    Random random = new Random();

    while(lotteryDraw.size()<6) {
        lotteryDraw.add(random.nextInt(max-min)+min);
    }

    lotteryDraw.forEach(System.out::println);
}

Comments

0

Use a set if you want to avoid duplicate values.

Example:

public static Set <Integer> drawLotto() { //The validation I have here I know isn't the most effective way and is-
    Random r = new Random(); //resource comsuning but this was the only way I could think of doing it.
    int draw[] = new int[6];
    int min = 1;
    int lotteryMax = 10;
    Set<Integer> lotteryDraw = new HashSet<Integer>();
    for (int i = 0; i < draw.length; i++) {
        draw[i] = r.nextInt(lotteryMax - min) + min;
        lotteryDraw.add(draw[i]);
    }
    int size = lotteryDraw.size();
    if (size != 6) {
        return drawLotto();
    } else {
        return lotteryDraw;
    }
}

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.