Possible Duplicate:
Generating Unique Random Numbers in Java
How can I generate random number between 0 and 1000 and keep on passing unique random number that got generated between 0 and 1000 to a particular method. So for that I generated the number between 0 and 1000 and inserted unique random number between 0 and 1000 in List so that we can compare whether the random number we are generating is already present in the list or not. If it is present then generate it again. But somehow I believe the below code will fail sometimes.
public class Testing4 {
private static List<Integer> randomNumber;
private static Random r = new Random();
private static int rand;
private static int endRange = 1000;
public static void main(String args[]) throws IOException {
randomNumber = new ArrayList<Integer>();
for (int i = 1; i<= endRange; i++) {
rand = r.nextInt(endRange);
if(randomNumber.contains(rand)) {
rand = r.nextInt(endRange);
} else {
randomNumber.add(rand);
}
// Pass the unique random number between 0 and 1000 to this method
randomNumberMethod(rand);
}
}
}