12likea - 1 month ago 4x
Java Question

Creating an automated Lottery

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?

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