Science, Tech, Math › Computer Science Generating Unique Random Numbers An ArrayList and the Shuffle method simulate a sequence with no repeats Share Flipboard Email Print (JGI/Tom Grill/Blend Images/Getty Images) Computer Science Java Programming PHP Programming Perl Python Javascript Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More By Paul Leahy Paul Leahy Computer Science Expert M.A., Advanced Information Systems, University of Glasgow Paul Leahy is a computer programmer with over a decade of experience working in the IT industry, as both an in-house and vendor-based developer. Learn about our Editorial Process Updated on February 05, 2019 When you generate random numbers it's often the case that each generated number number must 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 numbers = new ArrayList(); for(int i = 0; i < 40; i++) { numbers.add(i+1); } System.out.println(numbers); }} Note that we are 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 A utility class called Collections offers 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 objectsArrayList numbers = new ArrayList();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 objectsArrayList numbers = new ArrayList();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 Cite this Article Format mla apa chicago Your Citation Leahy, Paul. "Generating Unique Random Numbers." ThoughtCo, Feb. 16, 2021, thoughtco.com/generating-unique-random-numbers-2034208. Leahy, Paul. (2021, February 16). Generating Unique Random Numbers. Retrieved from https://www.thoughtco.com/generating-unique-random-numbers-2034208 Leahy, Paul. "Generating Unique Random Numbers." ThoughtCo. https://www.thoughtco.com/generating-unique-random-numbers-2034208 (accessed April 2, 2023). copy citation