Here is the hint my professor gave me in the description for this lottery simulation lab:
2. A little care is needed in the creation of the random winning ticket. You can’t just use a loop to
create k random numbers less than n, because you might get duplicates from the random number
generator. The easiest way to do it is this is to work with an array from which you successively
remove the numbers you have already picked. I’ll illustrate with k = 3 and n = 6:
Create an array of size n = 6 and initialize: 1 2 3 4 5 6
Choose a random index using Random(6), say 2.
Take 3 (remembering to start at 0) as your first number on the winning ticket.
Finally take the largest unused index (6), and move that number to 3,
leaving the array as 1 2 6 4 5
Choose a random index using Random(5), say 3.
Take 4 (start at 0!) as your second number on the winning ticket.
Now copy the largest unused index (5) to where 4 was,
leaving the array as 1 2 6 5
Now repeat using Random(4), etc.
By doing it this way, you guarantee that you are always selecting your next number from a pool
which does not contain any of the numbers already selected, so you can’t get a duplicate.
class 1 of 2 code:
/********************/
/* Tim Sewell Lab 8 */
/********************/
package LotterySimulator;
import javax.swing.JOptionPane;
import java.util.Scanner;
public class Lottery
{
/**********************************/
/* This program will simulate the */
/* operation of a lottery drawing */
/**********************************/
static int maxNumber, numbersToPick;
static boolean gDone = false;
public static void main(String[] args)
{
int winningTicket[], userTicket[];
splash();
setLotteryParameters();
Drawing draw = new Drawing(maxNumber, numbersToPick);
winningTicket = draw.makeRandomDraw();
userTicket = new int[numbersToPick];
while (!gDone)
{
userTicket = getUserTicket();
draw.sort(userTicket);
compareTicket(userTicket, winningTicket);
gDone = getUserIntent();
}
printWinner(winningTicket);
signOff();
}// end of main
private static void splash()
{
JOptionPane.showMessageDialog(null, "Welcome to the Sewell Lottery Application!");
}// end of splash
private static void setLotteryParameters()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the maximum number of lottery numbers:");
maxNumber = sc.nextInt();
System.out.println("Enter how many numbers will be on the ticket:");
numbersToPick = sc.nextInt();
}// end setLotteryParameters
private static boolean getUserIntent()
{
int n = JOptionPane.showConfirmDialog(
null,
"Would you like to check another lottery ticket?",
"Check your ticket",
JOptionPane.YES_NO_OPTION);
if (n == 1)
{
gDone = true;
}
if (n == 0)
{
gDone = false;
}
return gDone;
}
private static void printWinner(int[] winningTicket)
{
for (int i = 0; i < winningTicket.length; i ++)
System.out.printf("%d ", winningTicket[i]);
System.out.println();
}
private static int[] getUserTicket()
{
String userTicketStr = JOptionPane.showInputDialog(
"Enter your " + numbersToPick +
" lottery numbers in ACCENDING order" +
"separated by spaces:");
Scanner sc = new Scanner(userTicketStr);
int userTicket[] = new int[numbersToPick];
for (int i = 0; i < userTicket.length; i++){
userTicket[i] = sc.nextInt();
System.out.printf("%d ", userTicket[i]);
System.out.println();
}
return userTicket;
}
public static void compareTicket(int[] userTicket, int[] winningTicket)
{
}
private static void signOff()
{
JOptionPane.showMessageDialog(
null, "Thank You For Using The " +
"Sewell Lottery Application!");
}
}// end of class
class 2 of 2 code:
/********************/
/* Tim Sewell Lab 8 */
/********************/
package LotterySimulator;
import java.util.Random;
public class Drawing {
private int maxValue, pickCount;
private int result[] = new int[pickCount];
public Drawing(int maxNumber, int numbersToPick)
{
maxNumber = maxValue;
numbersToPick = pickCount;
}// end of drawing
public int[] makeRandomDraw()
{
Random random = new Random();
int maxIndex = maxValue - 1;
for (int i = 0; i < pickCount; i ++)
{
int randomPick = random.nextInt(maxIndex);
result[i] = randomPick;
}
for (int startScan = 0; startScan < (result.length - 1); startScan ++)
{
int minIndex = startScan;
int minValue = result[startScan];
for (int index = startScan + 1; index < result.length; index ++)
{
if (result[index] < minValue)
{
minValue = result[index];
minIndex = index;
}
}
result[minIndex] = result[startScan];
result[startScan] = minValue;
}
return result;
}// end of makeRandomDraw
public void sort(int[] userTicket)
{
}// end of sort
}// end of class
any help on how to code this algorithm would be greatly appreciated
Thanks,
Tim

New Topic/Question
Reply




MultiQuote





|