Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unduplicated random Numbers in an java array

Tags:

java

arrays

I've searched and simply cannot find an answer to my problem. I have to create a lotto type simulation, 6 numbers + bonus number with no duplicates. i have manged to get it working but in the 6 number array the number at poistion [1] comes out as 0 all the time.I'm sure its simple issue but I'm brand new to this so any help will be gratefully accepted.Code below, I'm sure its all over the place........

import java.util.Arrays;

public class Lotto {

    public static void main(String[] args) {

        int[] Lotto=new int[6];
        final int MIN = 1;
        final int MAX = 45;

        for (int i = 0; i< Lotto.length; ++i)
        {

            Lotto [0] =MIN+ (int)(Math.random()*((MAX -MIN)+1));

            while (Lotto[1] == Lotto[0])
            {
                Lotto[1] =MIN+ (int)(Math.random()*((MAX -MIN)+1));
            }

            while ((Lotto[2] == Lotto[0]) || (Lotto[2] == Lotto[1]) )
            {
                Lotto[2] = MIN+ (int)(Math.random()*((MAX -MIN)+1));

            while ((Lotto[3] == Lotto[0]) || (Lotto[3] == Lotto[1]) || (Lotto[3] == Lotto[2]) )
            {
                Lotto[3] = MIN+ (int)(Math.random()*((MAX -MIN)+1));
            }

            while  ((Lotto[4] == Lotto[0]) || (Lotto[4] == Lotto[1]) || (Lotto[4] == Lotto[2]) ||   (Lotto[4] == Lotto[3]) )                        

            {
                Lotto[4] = MIN+ (int)(Math.random()*((MAX -MIN)+1));



                while  ((Lotto[5] == Lotto[0]) || (Lotto[5] == Lotto[1]) || (Lotto[5] == Lotto[2]) ||   (Lotto[5] == Lotto[3])||    (Lotto[5] == Lotto[4])) 

                {
                    Lotto[5] = MIN+ (int)(Math.random()*((MAX -MIN)+1));
                }


                int[] BonusNumber=new int[1];
                for (int j = 0; j< BonusNumber.length; ++j)
                {

                    BonusNumber [j] =1+ (int)(Math.random()*((45 -1)+1));
                }

                System.out.println("Winner  "  + Arrays.toString(Lotto));
                System.out.println("Bonus Number" +Arrays.toString(BonusNumber));   
            {


    }
            }
            }
        }
    }
}
like image 584
user1958789 Avatar asked May 22 '26 13:05

user1958789


2 Answers

Change this loop:

while (Lotto[1] == Lotto[0])
{
    Lotto[1] = random(MIN, MAX);
}

into:

do {
    Lotto[1] = random(MIN, MAX);
} while (Lotto[1] == Lotto[0]);

And the rest of the code accordingly. Do you see where the problem lies?

But before you start rewriting your quite complex and unmaintainable code, check out this implementation, that is much easier to understand and... correct:

final int MIN = 1;
final int MAX = 45;

final List<Integer> allPossible = new ArrayList<Integer>(MAX - MIN + 1);
for (int i = MIN; i <= MAX; ++i) {
    allPossible.add(i);
}
Collections.shuffle(allPossible);
final List<Integer> lotto = allPossible.subList(0, 6);
int bonusNumber = allPossible.get(6);

System.out.println("Winner  "     + lotto);
System.out.println("Bonus Number" + bonusNumber);

Collections.shuffle() is crucial here.

like image 56
Tomasz Nurkiewicz Avatar answered May 25 '26 04:05

Tomasz Nurkiewicz


I think your code is quite confusing and you can avoid so much conditions by using some API utilities such as Collections.shuffle() as in this example:

public static void main(String[] args) 
{
    Integer[] numbers = new Integer[45];

    // Populating numbers.
    for (int i = 0; i < 45; i++)
        numbers[i] = i + 1;

    // Shuffling them to make them in random order.
    List<Integer> list = Arrays.asList(numbers);
    Collections.shuffle(list);

    // Print any 6 of them. I chose first 6 ones for simplicity.
    for (int i = 0; i < 6; i++)
        System.out.print(list.get(i) + " ");
}

Sample output:

15 11 31 2 5 38 
like image 28
Juvanis Avatar answered May 25 '26 02:05

Juvanis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!