0

I would like to program an automated Lottery "script" which creates 2 Arrays: one holding the Lottery numbers and another holding the guess itself. Then, I check if eg. lottery[] holds numbers of the guess[], if the numbers are equal and he hits the 6 right numbers in the loop, I want the console to print out how many tries he needed.

public static void main(String[] args) {

    int[] lottery = new int[6];
    int[] tipp1 = new int[6];
    int randomNum;
    int tipp;

    for (int i = 0; i < 6; i++) {
        randomNum = (int) (Math.random() * 49 + 1); // Random number created here.
        for (int x = 0; x < i; x++) {
            if (lottery[x] == randomNum) // Here, code checks if same random number generated before.
            {
                randomNum = (int) (Math.random() * 49 + 1);// If random number is same, another number generated.
                x = -1; // restart the loop
            }
        }
        lottery[i] = randomNum;
        System.out.print(lottery[i] + " ");
    }

    System.out.println(" ");

    for (int c = 0; c < 6; c++) {
        tipp = (int) (Math.random() * 49 + 1); // Random number created here.
        for (int xy = 0; xy < c; xy++) {
            if (tipp1[xy] == tipp) // Here, code checks if same random number generated before.
            {
                tipp = (int) (Math.random() * 49 + 1);// If random number is same, another number generated.
                xy = -1; // restart the loop
            }
        }
        tipp1[c] = tipp;
        System.out.print(tipp1[c] + " ");
    }
    if (lottery.equals(tipp1)){
        System.out.print("HIT!");
    }
}
}

Is there a way to do this?

Cshell
  • 19
  • 1
  • 8
12likea
  • 3
  • 2
  • 2
    Possible duplicate of [Comparing two integer arrays in java](http://stackoverflow.com/questions/14897366/comparing-two-integer-arrays-in-java) – Jonny Henly Sep 20 '16 at 01:34
  • 2
    Using the equals method on the array won't work, by the way. I'm not sure that's the only problem, though – OneCricketeer Sep 20 '16 at 01:35
  • I'd suggest learning about methods. You have two functionally identical blocks of code that generate an array of random numbers. You can read about [DRY](https://en.m.wikipedia.org/wiki/Don%27t_repeat_yourself) programming – OneCricketeer Sep 20 '16 at 01:40
  • 1
    what exactly is your end goal? Your code looks complete to me. Which part are you having difficulty with? – java_doctor_101 Sep 20 '16 at 01:57
  • there should be a continous loop that "searches" for the right numbers generated in the lorrery Array. Oh and thank you about the DRY thing. I will try to work towards it! – 12likea Sep 20 '16 at 02:01

2 Answers2

3

So the way I understand your question is as follows: You want a program that generates a set of 6 random numbers each number within the range 1 to 50 - this represents the correct lottery numbers. You then want to have the program generate random sets of 6 numbers each of which is within the range 1 to 50 until one of these sets matches the lottery numbers. You also want the program to keep track of how many guesses were made before a win was achieved - I am assuming that this means lottery numbers are chosen once and guesses are made continually until a match occurs.

Note: I do not see logic in your code sample for tracking the number of guesses, but I did implement this feature in my code below.

It is not clear whether the order of the lottery numbers is important. I wrote the code below such that the order is not important, meaning that as long as the guess contains all 6 of the lottery numbers it is considered a winner. If you want order to be important you should remove the call to Arrays.sort(...)

Here is a solution that uses arrays since you specifically said you wanted to use arrays:

import java.util.Arrays;
import java.util.Random;

public class LotteryMain {
    public static void main(String[] args) {

        int[] correctNumbers = createSortedArrayOfRandomInts(6);
        System.out.println("Lottery numbers: "+Arrays.toString(correctNumbers));

        int tries = 0;
        int[] guessNumbers = null;
        do{
            guessNumbers = createSortedArrayOfRandomInts(6);
            System.out.println("Guessed numbers: "+Arrays.toString(guessNumbers));
            tries++;
        }while(!Arrays.equals(guessNumbers, correctNumbers));

        System.out.println("Lottery numbers: "+Arrays.toString(correctNumbers));
        System.out.println("WINNER! Number of tries: "+tries);
    }

    public static int[] createSortedArrayOfRandomInts(int sizeOfArray){
        int[] arrayOfRandomNumbers = new int[sizeOfArray];
        Random randomGen = new Random();
        for (int i = 0; i < sizeOfArray; i++) {
            int randomNum = -1;
            do{
                randomNum = randomGen.nextInt(50)+1; // Random number created here.
            }while(contains(arrayOfRandomNumbers, randomNum));
            arrayOfRandomNumbers[i] = randomNum;
        }
        Arrays.sort(arrayOfRandomNumbers);
        return arrayOfRandomNumbers;
    }

    public static boolean contains(int[] array, int searchFor){
        for(int i = 0; i < array.length; i++){
            if(array[i] == searchFor)
                return true;
        }
        return false;
    }

}

Here is sample output (shortened, obviously):

...
Guessed numbers: [5, 21, 27, 36, 40, 47]
Guessed numbers: [3, 14, 15, 28, 36, 42]
Guessed numbers: [24, 27, 31, 32, 44, 49]
Guessed numbers: [19, 21, 25, 39, 42, 46]
Lottery numbers: [19, 21, 25, 39, 42, 46]
WINNER! Number of tries: 2499043

Here is a solution using classes from the Java Collections Framework because every Java programmer should be familiar with how to use these classes:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class LotteryWithCollections {

    public static void main(String[] args) {

        List<Integer> correctNumbers = createSortedListOfRandomInts(6);
        System.out.println("Lottery numbers: " + correctNumbers);

        int tries = 0;
        List<Integer> guessNumbers = null;
        do {
            guessNumbers = createSortedListOfRandomInts(6);
            System.out.println("Guessed numbers: " + guessNumbers);
            tries++;
        } while (!guessNumbers.equals(correctNumbers));

        System.out.println("Lottery numbers: " + correctNumbers);
        System.out.println("WINNER! Number of tries: " + tries);
    }

    public static List<Integer> createSortedListOfRandomInts(int sizeOfList) {
        List<Integer> listOfRandomNumbers = new ArrayList<Integer>();
        Random randomGen = new Random();
        for (int i = 0; i < sizeOfList; i++) {
            int randomNum = randomGen.nextInt(50) + 1; // Random number created
                                                        // here.
            while (listOfRandomNumbers.contains(randomNum)) {
                randomNum = randomGen.nextInt(50) + 1;
            }
            listOfRandomNumbers.add(randomNum);
        }
        Collections.sort(listOfRandomNumbers);
        return listOfRandomNumbers;
    }
}

Something else to consider - right now the code allows for duplicate guesses, it might be interesting to keep track of previous guesses and disallow repeats of the same guess.

D.B.
  • 4,523
  • 2
  • 19
  • 39
  • WOW! Thats so awesome. Im sure i will learn from this and i will try to figure out how this code works line by line. – 12likea Sep 20 '16 at 03:15
0

So I think I just did your homework for you ... but here is some source code that does what I think you were going for. I don't know the exact constraints of your lotto though.

You need to learn about DRY as stated in the comments this is demonstrated by my use of the random array static method. Also based on your source code you should learn about variable scoping in java. If you want me source to run until there is a hit that is pretty simple while loop logic.

package lotto;
/**
 * @author czarking
 */
public class Lotto {

    private static int[] lottery = new int[6];
    private static int[] guess = new int[6];

    public static void main(String[] args) {
        lottery = randomArray(lottery);
        guess = randomArray(lottery);
        boolean match = true;
        for (int i=0; i<lottery.length; i++) {
            if (lottery[i] != guess[i])
                match = false;
        }
        if (match)
            System.out.println("HIT!");
    }

    public static int[] randomArray(int[] ar) {
        for (int i=0; i<ar.length; i++) {
            ar[i] = (int)(Math.random()*49 + 1);
            System.out.println(ar[i]+" ");
        }
        return ar;
    }

}
qxz
  • 3,814
  • 1
  • 14
  • 29
Czarking
  • 143
  • 1
  • 10
  • now i get it. thank you for the help! But there is one thing. If eg. the guess is right at guess[2] for lottery[1] he wont give me a hit right? I will try to figure it out and hopefully learn on that one! Its not any homework btw. :) i think this is a great way to learn on Arrays. – 12likea Sep 20 '16 at 03:04
  • This code allows for duplicates inside the array generated by the `randomArray` method. The code in the question does not. – D.B. Sep 20 '16 at 03:18
  • @12likea the way the for loop is written if any of the any of the values do not match up the variable will be false – Czarking Sep 20 '16 at 04:36
  • @D.B. I know I was demonstrating DRY and scope it seemed to me that he already had a heuristic to prevent number duplication and there is much content online has to how to do that but if adding that code will get up votes I suppose I could add a few lines – Czarking Sep 20 '16 at 04:39