How to Generate Random Numbers Without Repetition in Java

Save

The Java language provides utilities for generating random numbers within your programs. If you want to generate a series of random numbers, but do not want any of them to be repeated, you need to use conditional tests and loops. By adding each number you generate to a set collection object, you can tell immediately whether any of them are duplicates. If the number is a duplicate, you can instruct Java to continue generating new numbers until it finds one you have not already added to the set. The process involves two loops and a conditional statement.

  • Import the Java utilities necessary for your random number generation and set object. Add the following code at the top of your class declaration:
    import java.util.*;

    Create a set object to store the numbers you generate:
    TreeSet myNumbers = new TreeSet();

    Create an instance of the random class to generate your random numbers:
    Random randGen = new Random();

  • Create a loop to generate your random numbers. The following for loop will execute 20 times:
    for(int i=0; i<20; i++) {
    //number generation here
    }

    Inside this loop, you can add the processing to generate and check your random numbers for duplicates. Alter the number 20 to reflect the total number of random values you want your program to generate. Inside the loop, generate a random integer:
    int randNum = randGen.nextInt();

    This will execute each time the loop iterates.

  • Add a second loop to cope with duplicate numbers. Add the following code inside your first loop after the number generation line:
    for(;;) {
    //deal with duplicates
    }

    This is an infinite loop, so it will execute endlessly until the code explicitly breaks out of it. Inside this loop, you can instruct Java to continue generating random numbers until it generates one that you have not already used.

  • Add a conditional test on each random number. Insert the following code inside your infinite loop:
    if(myNumbers.add(randNum)) break;
    else randNum = randGen.nextInt();

    When you try to add an element to a set, the method returns true or false. If the element is not already in the set, it returns true, in which case your code can break out of the loop. If the element is already in the set, the method returns false, in which case you can instruct Java to choose another random number. The loop will then execute again, testing the new value to see if it is a duplicate. This process continues until a new value is found.

  • Test your code. Add the following line after your infinite loop, but still inside the outer loop:
    System.out.println(randNum);

    This will let you see at a glance that your numbers are all unique. Save your file, compile your program and run to test it. Browse through the output to check that your functions are working.

Tips & Warnings

  • You can generate random numbers within a certain range by passing a parameter to the "nextInt" method.
  • If you are generating numbers within a certain range, make sure your for loop is not set to execute more times than the possible values within that range.

References

  • Photo Credit Jupiterimages/Pixland/Getty Images
Promoted By Zergnet

Comments

Resources

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!