简体   繁体   中英

Random numbers in an array without duplicates

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. 

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

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);
}

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM