I'm in the process of writing a program that generates six random numbers. If I run the program I have so far, there's a bit of a problem. It is possible for this code to generate the same random number more than once. Generally speaking, lottery tickets require the numbers to be unique.

I know I need to modify Part1.java so that the generate method does not create duplicate numbers. I'm guessing it is easiest to get this right by adding additional static methods, rather than
putting all the code inside the existing loop? I'm really not so sure how I should go about doing this though. I've also attached the program for you guys to take a look at. I'd really appreciate any suggestions or feedback. Thank you!

/**
* Part1.java
*
*/


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


public class Part1
{
/**
* There are 6 numbers in each lottery ticket
*/
public static final int TOTAL_NUMBERS   = 6;


/**
* The smallest number on the ticket is 1
*/
public static final int MIN_NUMBER  = 1;


/**
* The largest number on the ticket is 49
*/
public static final int MAX_NUMBER  = 49;


/**
* This method populates the array a with numbers
* between MIN_NUMBER and MAX_NUMBER
*
* At present, it does not check for duplicates.
*/
public static void generate (int[] a)
{
/* FIXME: remove duplicates */


Random r = new Random();


for (int i=0;i<TOTAL_NUMBERS;i++)
{
/**
* NB: r.nextInt returns a number that is
*     >= 0 and < MAX_NUMBER
*
* So this statement generates a number between 1 and
* 49.
*
* See:
* http://java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html
*/
a = r.nextInt(MAX_NUMBER)+MIN_NUMBER;
}


}


/**
* This method prints all the elements of an array
* on one line enclosed in {}.
*/
public static void display (int[] n)
{
System.out.print("{");
for(int i=0;i<n.length;i++)
{
System.out.print(n);
if (i!=n.length-1)
{
System.out.print(",");
}
}
System.out.println("}");
}


/**
* This method is where the program starts.  It
* allocates an array, populates the array
* with random numbers, sorts those numbers
* and finally displays the contents of the array.
*/
public static void main (String[] args)
{
int[]   theNums = new int[TOTAL_NUMBERS];


generate(theNums);


/* This is a utility function from java.util.Arrays
* that sorts the contents of the array in
* ascending order.
*/
Arrays.sort(theNums);


display(theNums);


}
}

Recommended Answers

All 2 Replies

As each number is generated, check the current numbers array to make sure it does not already contain that number, if it does, just generate another.

As each number is generated, check the current numbers array to make sure it does not already contain that number, if it does, just generate another.

I would like to add another suggestion. If I understood correctly, you have a pot with 49 numbers and you want to randomly select 6. When you select the 1st there are 48 left from which you want to select 5.
So I don't think it is 100% right to select again a random number from 1 to 49 and if it like the one already selected, to generate another. Because you want after the 1st selection the remaining 48 numbers to have the same possibility to be selected.

So here is my suggestion:
Have a Vector with 49 numbers and always generate a random number from 0 to Vector.size(49). Once generated, remove that number from the Vector and generate again another random number from 0 to Vector.size(48).
The number generated will be actually the index of the element in the Vector, so after a few loops they won't match (The number at index 3 will not be the number '3') but that is not important because the index and the selection will be random so you don't care how the numbers are sorted in the Vector.

Another way is to use an array of 49 numbers and when one of them is selected, "swap" it with the last element and randomly take a number from the first 48:

N = 48
ARRAY: {1,2,....49}
loop
  take a random index from 0 to N
  number = ARRAY[index]
  swap the number at the "index" with the number at the "N" position
  N = N-1
end loop

Of course Ezzaral's suggestion is much simpler and easier to understand and if your teacher will not give any trouble try Ezzaral suggestion.

PS: Where are you from OracleLite because that lottery is exactly the Lotto we have in my country Greece (49 numbers and 6 are selected)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.