# Thread: Lottery - Non-Repeating Random Numbers from Array

1. Member
Join Date
Jan 2012
Location
Aberdeen
Posts
5
Rep Power
0

## 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:

Java 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] + ", ");
}

}

}```

2. ## 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?

3. Senior Member
Join Date
Aug 2011
Posts
251
Rep Power
6

## 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.

4. Member
Join Date
Jan 2012
Location
Aberdeen
Posts
5
Rep Power
0

## 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!

5. ## 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

Java Code:
```		while (set.size() < n) {
rand = (int)(Math.random()*o)+1;
}```
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]);
Last edited by j2me64; 01-27-2012 at 12:21 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•