1. Technology

Generating Unique Random Numbers

By

Businessman working in office
JGI/Tom Grill/Blend Images/Getty Images

Once you know how to generate random numbers it's often the case that the numbers also need to be unique. A good example is picking lottery numbers. Each number picked randomly from a range (e.g., 1 to 40) must be unique otherwise the lottery draw would be invalid.

Using a Collection

The easiest way to pick unique random numbers is to put the range of numbers into a collection called an ArrayList. If you've not come across an ArrayList before it's a way of storing a set of elements that don't have a fixed number. The elements are objects that can be added to or removed from the list. For example, let's make the lottery number picker. It needs to pick unique numbers from a range of 1 to 40.

First put the numbers into an ArrayList using the add() method. It takes the object to be added as a parameter:

 import java.util.ArrayList;
 public class Lottery {
 
   public static void main(String[] args) {
 
   //define ArrayList to hold Integer objects
   ArrayList<Integer> numbers = new ArrayList<Integer>();
   for(int i = 0; i < 40; i++)
   {
     numbers.add(i+1);
   }
   System.out.println(numbers);
 } 

Note that I'm using the Integer wrapper class for the element type so that the ArrayList contains objects and not primitive data types.

The output shows the range of numbers from 1 to 40 in order:

 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40] 

Using the Collections Class

There is a utility class called Collections that provides different actions that can be performed on a collection like an ArrayList (e.g., search the elements, find the maximum or minimum element, reverse the order of elements, and so on). One of the actions it can perform is to shuffle the elements. The shuffle will randomly move each element to a different position in the list. It does this by using a Random object. This means it's a deterministic randomness, but it will do in most situations.

To shuffle the ArrayList, add the Collections import to the top of the program and then use the Shuffle static method. It takes the ArrayList to be shuffled as a parameter:

 import java.util.Collections;
 import java.util.ArrayList;
 public class Lottery {
 
   public static void main(String[] args) {
 
     //define ArrayList to hold Integer objects
     ArrayList<Integer> numbers = new ArrayList<Integer>();
     for(int i = 0; i < 40; i++)
     {
       numbers.add(i+1);
     }
 
     Collections.shuffle(numbers);
     System.out.println(numbers);
   }
 } 

Now the output will show the elements in the ArrayList in a random order:

 [24, 30, 20, 15, 25, 1, 8, 7, 37, 16, 21, 2, 12, 22, 34, 33, 14, 38, 39, 18, 36, 28, 17, 4, 32, 13, 40, 35, 6, 5, 11, 31, 26, 27, 23, 29, 19, 10, 3, 9] 

Picking the Unique Numbers

To pick the unique random numbers simply read the ArrayList elements one by one by using the get() method. It takes the position of the element in the ArrayList as a parameter. For example, if the lottery program needs to pick six numbers from the range of 1 to 40:

 import java.util.Collections;
 import java.util.ArrayList;
 public class Lottery {
 
   public static void main(String[] args) {
 
     //define ArrayList to hold Integer objects
     ArrayList<Integer> numbers = new ArrayList<Integer>();
     for(int i = 0; i < 40; i++)
     {
     numbers.add(i+1);
   }
 
     Collections.shuffle(numbers);
     System.out.print("This week's lottery numbers are: ");
     for(int j =0; j < 6; j++)
     {
       System.out.print(numbers.get(j) + " ");
     }
   }
 } 

The output being:

 This week's lottery numbers are: 6 38 7 36 1 18 
  1. About.com
  2. Technology
  3. Java
  4. Java Syntax
  5. Utilizing the Java API
  6. java.util
  7. Random Numbers: Generating Unique Random Numbers

©2014 About.com. All rights reserved.