# Lottery - Non-Repeating Random Numbers from Array

• 01-27-2012, 03:10 AM
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] + ", ");       }   }     }```
• 01-27-2012, 03:20 AM
kjkrum
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?
• 01-27-2012, 03:20 AM
tnrh1
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.
• 01-27-2012, 11:56 AM
```                while (set.size() < n) {                         rand = (int)(Math.random()*o)+1;                         set.add(rand);                 }```