# Non-Repeating Random Integers

• 01-31-2010, 07:18 PM
Psyclone
Non-Repeating Random Integers
I trying to make a list of 100 random numbers between 1 and 100 without any of them repeating. I built a boolean array and assigned the value true as each number was chosen inside a for-if loop.

The problem is that sometimes it takes about 600-800 iterations.

I was wondering if there was a better way to do this. Is there a method to do this already in Java that isn't as intensive?

I was thinking of making a list of 100 random numbers and then just ranking them and using the ranking as my 100 random numbers since I wouldn't have any repeating. I'm not sure if there is a way to rank a list of 100 numbers though or how intensive it would be.
• 01-31-2010, 07:23 PM
JosAH
Quote:

Originally Posted by Psyclone
I trying to make a list of 100 random numbers between 1 and 100 without any of them repeating. I built a boolean array and assigned the value true as each number was chosen inside a for-if loop.

The problem is that sometimes it takes about 600-800 iterations.

I was wondering if there was a better way to do this. Is there a method to do this already in Java that isn't as intensive?

I was thinking of making a list of 100 random numbers and then just ranking them and using the ranking as my 100 random numbers since I wouldn't have any repeating. I'm not sure if there is a way to rank a list of 100 numbers though or how intensive it would be.

Have a look at the Collections.shuffle( ... ) method. If you want to generate n random numbers from a set much larger than n it works to generate a possible number, check if it is used already, and if so try again. This method gets slower and slower when the size of set set is nearer to n. e.g. the last number from your set only has a probability of 1/100 for not being used already. You don't want to retry and retry until you have randomly found that one unused number.

kind regards,

Jos
• 01-31-2010, 07:59 PM
collin389
Why cant you just make a list that goes 1-100 and shuffle it using Collections.shuffle?
• 01-31-2010, 08:22 PM
Psyclone
Thanks for the help. I actually did end up making a list from 1-100, shuffling it, then adding the value to my array.

It works great.
• 01-31-2010, 09:00 PM
Lil_Aziz1
Another way of doing this is to make an ArrayList.

Code:

```import java.util.ArrayList; import java.util.Random; public class randomGenerator {                 private ArrayList<Integer> generate;         private final int INITIAL_SIZE = 100;                 public randomGenerator() {                 generate = new ArrayList<Integer>(INITIAL_SIZE);                 fillList();         }                 //Each element will store a number 0-99         private void fillList() {                 for (int i=0; i<INITIAL_SIZE; i++)                         generate.add(i);         }                 public void printRandomInt() {                 Random pseudoNumber = new Random();                 int getThisElement;                                 for (int i=0; i<INITIAL_SIZE; i++) {                         getThisElement = pseudoNumber.nextInt(generate.size());                         System.out.print(generate.remove(getThisElement) + " ");                 }                         } }```
Code:

```public class randomGenerateDriver {         public static void main(String[] args) {                 randomGenerator test = new randomGenerator();                 test.printRandomInt();         } }```
• 01-31-2010, 10:04 PM
collin389
Yes lil_aziz, that is sort of like the fisher-yates shuffle, but you dont need an array list, just and array, if you were going to use a list, why not just use collections.shuffle()?
Fisher-Yates shuffle - Wikipedia, the free encyclopedia
There is a java implementation of this shuffle on that Wiki page.