Results 1 to 5 of 5
  1. #1
    adize is offline Member
    Join Date
    Jan 2012
    Location
    Aberdeen
    Posts
    5
    Rep Power
    0

    Default 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. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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?
    Get in the habit of using standard Java naming conventions!

  3. #3
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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. #4
    adize is offline Member
    Join Date
    Jan 2012
    Location
    Aberdeen
    Posts
    5
    Rep Power
    0

    Default 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. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default 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;
    			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]);
    Last edited by j2me64; 01-27-2012 at 11:21 AM.

Similar Threads

  1. Random Lottery Numbers
    By ComicStix in forum New To Java
    Replies: 2
    Last Post: 04-29-2011, 06:18 AM
  2. Replies: 7
    Last Post: 04-22-2011, 11:40 AM
  3. Array of random numbers ...
    By shane1987 in forum New To Java
    Replies: 31
    Last Post: 11-14-2010, 09:33 PM
  4. Store Random numbers into Array
    By abby0910 in forum New To Java
    Replies: 19
    Last Post: 07-12-2010, 12:59 AM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 AM

Posting Permissions

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