1. Member
Join Date
Jan 2010
Posts
81
Rep Power
0

## 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.

2. 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

3. Senior Member
Join Date
Nov 2009
Posts
235
Rep Power
5
Why cant you just make a list that goes 1-100 and shuffle it using Collections.shuffle?

4. Member
Join Date
Jan 2010
Posts
81
Rep Power
0
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.

5. Another way of doing this is to make an ArrayList.

Java 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++)
}

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) + " ");
}

}
}```
Java Code:
```public class randomGenerateDriver {
public static void main(String[] args) {
randomGenerator test = new randomGenerator();
test.printRandomInt();

}
}```

6. Senior Member
Join Date
Nov 2009
Posts
235
Rep Power
5
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.

#### Posting Permissions

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