Lottery - Non-Repeating Random Numbers from Array

Hi everyone,

I'm quite new to Java and I hope that you can help me with some Java issues. Right now, I am practising arrays and I've got some problems with one lab exercise. I was looking for some hints, and I found some, however I still struggle to make my class work.

I was given a task to create a class that picks 6 numbers out of 49 for the National Lottery and stores them in an array.

I've written a method which keeps randomly picked numbers in a new array, but I don't know how to make those picked numbers non-repeating...

It would be great if you could give me some tips or ideas how to make it the way it should be. And if you see any mistakes in syntax please let me know.

Here's my code:

Code:

`import java.util.Random; `

/**

* A class that generates a set of numbers for the National Lottery and stores them in an array.

* @author AP

* @version 1.0 25/01/2012

*/

public class NationalLottery{

//===============Fields===============

//an array of 49 integers

private int[] numbers;

//an array of 6 integers

private int[] store;

//random number generator

private Random rand;

int i;

//===============Constructors===============

/**

* constructs an array of numbers 1-49

*/

public NationalLottery()

{

//declare new random object

rand = new Random();

numbers = new int[49];

for(int i=0; i<numbers.length;i++)

{

numbers[i]=i+1;

}

}

//===============Getters===============

/**

* @return number

*/

public int getNumber(int i)

{

return numbers[i];

}

//===============Methods===============

/**

* picks 6 random numbers from an array

*/

public void pickNumbers()

{

store = new int[6];

for(i=0;i<store.length;i++)

{

int number=rand.nextInt(numbers.length)+1;

store[i]=number;

}

}

/**

* prints out all values picked from the array

*/

public void printPicked()

{

System.out.println("Picked Numbers: ");

for(int i = 0; i<store.length; i++)

{

System.out.print(store[i] + ", ");

}

}

}

Re: Lottery - Non-Repeating Random Numbers from Array

So your instructor has told you to store the chosen numbers in an array. You didn't say if they have to *come from* an array...

Check out the Collections utility class, particularly the shuffle methods. Have you been taught about Lists yet?

Re: Lottery - Non-Repeating Random Numbers from Array

Make a method named isExist, it will recieve as parameters an array and an int.

it will returns true if that int found in the array else false.

Now on the pickNumbers() method you will make a while loop that runs while isExist(array,yourRandomResult) is true.

In while body you will save the random reuslt in an int.

So when you will finish that loop you will have a random number that not exists in the array.

*yea that would be a nested loop.

Re: Lottery - Non-Repeating Random Numbers from Array

Yes, they have to come from a bigger array as well. I haven't been taught about lists yet, so I will try to implement methods given by @rnth1 and I'll write soon if I managed to do it :) Thank you!

Re: Lottery - Non-Repeating Random Numbers from Array

in addition to the method shuffle() its also possible to use a class of the interface set. assuming there is a reference to a HashSet called set and two parameters n and o used as "n numbers out of o" the algorith could look like this

Code:

` while (set.size() < n) {`

rand = (int)(Math.random()*o)+1;

set.add(rand);

}

the while loop will run until the set hasn't the number of numbers given as argument and because the add() method of an hashset doesn't allow any duplicates the list will contain distinct numbers. to convert the set to an array of integers you can use

(Integer[])set.toArray(new Integer[n]);