Problem using an algorithm in an array

need to simulate a lottery drawing but can't figure out how to use

Page 1 of 1

11 Replies - 1284 Views - Last Post: 09 December 2008 - 04:22 PM Rate Topic: -----

#1 tmsewell2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-November 08

Problem using an algorithm in an array

Posted 08 December 2008 - 03:36 PM

This lab is designed to simulate a lottery drawing. The problem I am running in to is that my professor wants us to use an algorithm within an array to make sure there aren't any duplicate numbers drawn for the winning lottery numbers.

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

Is This A Good Question/Topic? 0
  • +

Replies To: Problem using an algorithm in an array

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Problem using an algorithm in an array

Posted 08 December 2008 - 03:47 PM

Your professor's guidance intrigues me.

I would have used a vector, but this is just as good. Here is my interpretation along with my test driver. It outputs all zeros at the end signifying all selections have been made.

import java.io.*;
import java.lang.Math.*;

public class ArrayRandom {
	
	public static void main(String[] args){
		
		int selections = 0, totalInArr = 6;
		int[] lottoChoice = new int[6]; 
		for (int i = 0; i < 6; i++)
		{
			lottoChoice[i] = i+1; //1-6 in the array now 
		}
		
		int temp;
		
		for(; selections < 6; selections++, totalInArr--)
		{
			temp = (int)Math.random() % totalInArr; //index 0-5
			//assign temp to some lotto winning ticket array or whatever
			lottoChoice[temp] = lottoChoice[totalInArr-1];
			lottoChoice[totalInArr-1] = 0; //make it zero in case of expandibility or whatever direction you take with this project
		}
		
		for(int i = 0; i < 6; i++)
		{
			System.out.print(lottoChoice[i]);
		}
		
	}
}



edited because code=java tags are indenting properly

This post has been edited by KYA: 08 December 2008 - 03:48 PM

Was This Post Helpful? 0
  • +
  • -

#3 tmsewell2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-November 08

Re: Problem using an algorithm in an array

Posted 08 December 2008 - 06:44 PM

Thanks for the reply. I see where you are going with that, but my professor lays out the structure of the lab in a handout, including methods and variables. I need to have the algorithm fit in here:
 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;
		}
		
		return result;
	}// end of makeRandomDraw



maybe i'm just missing how your code fits in? Sorry, I'm new at all this.
Thanks,
Tim
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Problem using an algorithm in an array

Posted 08 December 2008 - 07:13 PM

Just throw it into the function. However your code suggests that we really don't need t be doing this. We just generate a random number between 0 and the max to put as a lottery # selection.. anyways:

 public static int[] makeRandomDraw(int[] lottoChoice)
	{
		int[] result = new int[6];
		int selections = 0, totalInArr = 6;
	 
	int temp;
	int i = 0;	
	for(; selections < 6; selections++, totalInArr--, i++)
	{
		temp = (int)(Math.random() % totalInArr); //index 0-5
		result[i] = lottoChoice[temp];
		lottoChoice[temp] = lottoChoice[totalInArr-1];
		lottoChoice[totalInArr-1] = 0; //make it zero in case of expandibility or whatever direction you take with this project
	}
		return result;
	}// end of makeRandomDraw


Was This Post Helpful? 0
  • +
  • -

#5 tmsewell2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-November 08

Re: Problem using an algorithm in an array

Posted 08 December 2008 - 07:26 PM

ok. it seems to be working now. but i would like to get the winning selection to print out? for some reason it isn't printing to the console.
public int[] makeRandomDraw() 
	{
		System.out.println("hello");

		int selections = maxValue, totalInArr = pickCount;
		for (int i = 0; i < pickCount; i++)
		{
			result[i] = i+1;
		}
		System.out.println("hello");

		int temp;
	   
		for(; selections < pickCount; selections++, totalInArr--)
		{
			temp = (int)Math.random() % totalInArr; 
			result[temp] = result[totalInArr-1];
			result[totalInArr-1] = pickCount; 
		}
		System.out.println("hello");

		for(int i = 0; i < pickCount; i++)
		{
			System.out.println(result[i]);
		}
		System.out.println("hello");

		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


in this example i get 4 hello's to print to the console, but the " System.out.println(result[i]);" won't do anything...
Thanks,
Tim
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Problem using an algorithm in an array

Posted 08 December 2008 - 07:29 PM

The function takes no parameters, how does it know what result even is?
Was This Post Helpful? 0
  • +
  • -

#7 tmsewell2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-November 08

Re: Problem using an algorithm in an array

Posted 08 December 2008 - 07:50 PM

the values for maxValue and pickCount are sent as arguments and assigned to private variables aren't they used into the method?
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Problem using an algorithm in an array

Posted 08 December 2008 - 10:49 PM

int selections = maxValue, totalInArr = pickCount;



That is invalid. The function has no idea what maxValue, etc... are. Are you using a function that takes parameters, but didn't post it?
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: Problem using an algorithm in an array

Posted 09 December 2008 - 05:50 AM

Found a bug:
public Drawing(int maxNumber, int numbersToPick) {
	maxNumber = maxValue;
	numbersToPick = pickCount;
}



You're not assigning values to the object, you're assigning object values to the parameters you've passed. Which, btw, makes no sense. Also, I tend to dislike renaming those variables; it generally causes confusion, like above.

Since KYA's offered a close solution that I don't think is quite right... ;)

Here's how I'd implement your class. Please read and understand the comments. The could be optimised slightly, but I went for clarity instead.

class Drawing {
	private int maxValue, pickCount;
	// can't intialize this quite yet, you don't have pickCount
	//private int result[] = new int[pickCount];
	// nevermind, that shouldn't be an object variable.  Leave it.

	public Drawing(int maxValue, int pickCount) {
		// note, same names, explicit assignment to object instance value
		this.maxValue  = maxValue;
		this.pickCount = pickCount;
	}

	public int[] makeRandomDraw() {
		// this could be global to object, leaving for now
		java.util.Random random = new java.util.Random();
		// huh?
		//int maxIndex = maxValue - 1;
		
		// initialize, start with one
		int valuePool[] = new int[this.maxValue];
		for (int i = 0; i < this.maxValue; i++) { valuePool[i] = i+1; }

		// note, results are for this method only
		// we want to be able to call it more than once
		int result[] = new int[this.pickCount];
		
		int currentPoolSize = this.maxValue;
		for (int i = 0; i < this.pickCount; i ++) {
			// we're choosing a position in the pool
			int randomPos = random.nextInt(currentPoolSize);
			
			// the result is the value in that position
			result[i] = valuePool[randomPos];
			
			// replace the value we chose from the pool
			// with the last value ( which we didn't choose )
			// note that if we happened to choose the last one
			// this will basically have no effect, which is good.
			valuePool[randomPos] = valuePool[currentPoolSize - 1];
			
			// the pool is now smaller by one
			currentPoolSize--;
		}
		
		return result;
	}

	// don't understand, has nothing to do with class
	//public void sort(int[] userTicket) { }
}



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#10 tmsewell2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-November 08

Re: Problem using an algorithm in an array

Posted 09 December 2008 - 11:10 AM

Thanks, the over-commenting actually helps since I'm still pretty green
Was This Post Helpful? 0
  • +
  • -

#11 tmsewell2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-November 08

Re: Problem using an algorithm in an array

Posted 09 December 2008 - 04:20 PM

Thanks!!! extremely helpful! Some of the things that were in my previous code are set by the instructor, that's why you may have been confused about them being there, I know I was :)
Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Problem using an algorithm in an array

Posted 09 December 2008 - 04:22 PM

Quote

Since KYA's offered a close solution that I don't think is quite right...


It probably is off a bit, I wrote it in the fast reply box.

edited for typos

This post has been edited by KYA: 09 December 2008 - 04:22 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1