/* have the user input 6 lotto numbers from 1..50 and then auto-generate a 1 million lotto * drawings and see how many numbers the user matched. Output the average number of matched * numbers (this should be a value much less than 1, so output this with some precision) * Use methods as much as possible. */ import java.util.*; // need Random and Scanner public class Lotto { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random g = new Random(); int[] user; int[] lotto; int numMatched = 0; user = getPicks(in); // get the user's 6 numbers, data verify that they are between 1..50 and no duplicates for(int i=0;i<1000000;i++) { lotto = generateLotto(g); // get 6 random numbers, data verify that there are no duplicates numMatched += compare(user, lotto); // returns the number that matched, add this to numMatched } double avgMatched = numMatched / 1000000.0; System.out.printf("Out of 1 million attempts, the user's numbers matched %d times for an average of %5.3f per drawing\n", numMatched, avgMatched); } // method to input the user picks (6 int values between 1 and 50 with no duplicates), uses data verification to ensure the // 6 numbers are in the proper range and unique public static int[] getPicks(Scanner in) { int[] temp = new int[6]; // array to store the 6 user numbers int i, userPick; boolean[] picked = new boolean[50]; // used to represent whether a given pick as been selected yet or not for(i=0;i<50;i++) picked[i] = false; // initialized to false to say that no number has yet been picked for(i=0;i<6;i++) // iterate 6 times to fill in the user array (temp) { do{ System.out.print("Enter your next lotto pick between 1 and 50 "); // get the next user pick userPick = in.nextInt(); userPick--; // convert from 1..50 to 0..49 to fit with the picked array }while(userPick<0||userPick>49||picked[userPick]); // data verify legal number temp[i] = userPick; // add new pick to temp array picked[userPick] = true; // set userPick to true to indicate that number has been selected } return temp; // return the user selected numbers } // method to generate 6 random numbers from 0 to 49 (in this range because of the picked array), data verifying that each // number is unique public static int[] generateLotto(Random g) { int[] temp = new int[6]; // the array to store the random numbers int i, randomPick; boolean[] picked = new boolean[50]; // array to store whether a given number has been generated yet or not for(i=0;i<50;i++) picked[i] = false; // initialized to false because no numbers have been generated yet for(i=0;i<6;i++) // generate 6 random numbers { do{ // data verification loop to make sure the new number is unique randomPick = g.nextInt(50); // generate a number from 0 to 49 (instead of 1 to 50) }while(picked[randomPick]); // data verify temp[i] = randomPick; // place new number in temp array picked[randomPick] = true; // set userPick to true to indicate that the given number has been generated } return temp; // return the randomly generated 6 numbers } // method to compare the two arrays of 6 numbers, u is the user's picks and l is the auto-generated lotto numbers public static int compare(int[] u, int[] l) { int count = 0; // count the number of matches for(int i=0;i<6;i++) // use two nested for loops so that we look at each u[i] and compare it to all 6 numbers in l for(int j=0;j<6;j++) // note that we are assured there are no duplicate numbers otherwise the logic becomes more complex if(u[i]==l[j]) count++; // on a match between the two arrays, count it return count; // return the total number of matches } }