E.popp
E.popp

Reputation: 21

How do I make this code shorter by using arrays

How do I make this code shorter by using Arrays? This is part of a larger code where I simulated the lottery. this specific portion is where the user numbers are generated and I cant seem to figure out how to make this into an Array.

public static void compypick()
{
    boolean done;
    Random  randomgen = new Random();
    winn0 = randomgen.nextInt(53) + 1;

    done = false;
    while (!done)
    {
        winn1 = randomgen.nextInt(53) + 1;
        if (winn1 != winn0)
            done = true;
    }
    done = false;
    while (!done)
    {
        winn2 = randomgen.nextInt(53) + 1;
        if ((winn2 != winn1) && (winn2 != winn0))
            done = true;
    }
    done = false;
    while (!done)
    {
        winn3 = randomgen.nextInt(53) + 1;
        if ((winn3 != winn2) && (winn3 != winn1) && (winn3 != winn0))
            done = true;
    }
    done = false;
    while (!done)
    {
        winn4 = randomgen.nextInt(53) + 1;
        if ((winn4 != winn3) && (winn4 != winn2) && (winn4 != winn1) && (winn4 != winn0))
            done = true;
    }
    done = false;
    while (!done)
    {
        winn5 = randomgen.nextInt(53) + 1;
        if ((winn5 != winn4) && (winn5 != winn3) && (winn5 != winn2) && (winn5 != winn1) && (winn5 != winn0))
            done = true;
    }
    System.out.printf ("Winning numbers: %d %d %d %d %d %d\n", winn0, winn1, winn2, winn3, winn4, winn5);
}

Upvotes: 2

Views: 80

Answers (5)

Ousmane D.
Ousmane D.

Reputation: 56423

Here's a solution in reducing your current code to some good extent:

String result = random.ints(6, 1, 54) // IntStream
                .distinct() // IntStream of distinct values
                .mapToObj(Integer::toString) // Stream<String>
                .collect(joining(" ", "Winning numbers: ", "")); // String
System.out.println(result);

If you want to persist the unique winning numbers after printing then you could do:

int[] res = random.ints(6, 1, 53) // IntStream
                  .distinct() // IntStream of distinct values
                  .toArray();

System.out.printf ("Winning numbers: %d %d %d %d %d %d\n", 
                res[0], res[1], res[2], res[3], res[4], res[5]);
// still have access to "res" later on for further processing...

Upvotes: 1

Anda S
Anda S

Reputation: 41

A simple solution using just arrays would be this:

public static boolean contains(int[] array, int x) {
    for (int i : array) {
        if (i == x) return true;
    }
    return false;
}

public static void main(String[] args) {

    Random randomgen = new Random();
    int[] winningNumbers = new int[6];

    for (int i = 0; i < 6; i++) {
        int winn = randomgen.nextInt(53) + 1;
        while(contains(winningNumbers, winn)){
            winn = randomgen.nextInt(53) + 1;
        }
        winningNumbers[i] = winn;
    }

    System.out.printf("Winning numbers: ");
    for (int i: winningNumbers) {
        System.out.printf("%d ", i);
    }
    System.out.println();
}

Upvotes: 1

zlakad
zlakad

Reputation: 1384

Since the domain set has only 53 elements, you can chose this approach:

public static void main(String[] args) {

        //populate 'box'
        //(you can do it only once in the static block)
        List<Integer> balls = new ArrayList<>();
        for (int i = 1; i <= 53 ; i++) {
            balls.add(i);
        }

        //shuffle the balls in the box
        Collections.shuffle(balls);

        //pick the first six elements
        List<Integer> userBalls = balls.subList(0, 6);

        //optionally you can sort the user balls
        userBalls.sort(Integer::compareTo);

        //print
        System.out.println(userBalls);
    }
}

Upvotes: 0

GBlodgett
GBlodgett

Reputation: 12819

If they need to be unique you can use a Set:

Set<Integer> set = new HashSet<>();
while(set.size() < 6) {
    set.add(randomgen.nextInt(53) + 1);
}

Or java 8+ you can use the ints() method to get a Stream and collect it to a Set:

Random random = new Random();
Set<Integer> set = random.ints(0, 54)                          
                         .distinct().limit(6)
                         .boxed().collect(Collectors.toSet()); 

Or:

Set<Integer> set = random.ints(6, 1, 53).distinct().boxed().collect(Collectors.toSet());

Sample output:

48 3 41 25 11 31  

Upvotes: 1

Sweeper
Sweeper

Reputation: 270980

Arrays are fine for this job, but I think sets are a better choice, as you want unique numbers. You should also use a for loop. You are basically repeating some operation five times.

Set<Integer> winningNumbers = new HashSet<>();
for (int i = 0 ; i < 5 ; i++) {
    while (true)
    {
        int number = randomgen.nextInt(53) + 1;
        if (winningNumbers.add(number)) { // "add" returns false if the element already exists in the set
            break;
        }
    }
}
// printing the numbers:
System.out.println("Winning numbers: " + winningNumbers);

Upvotes: 0

Related Questions