I want to create a method that will generate random and unique numbers from 0 to 999. What I've tried to do is to generate random unique numbers and then add them to a vector so that each time i call the method it will generate a random number and then compare it to the previously generated numbers and see if it's unique. If it's not, the process is repeated. Thats the idea but every time i run the program i get "array index out of range: 0".
I have initialized the vector at the beginning of the class as private Vector<Integer> uniqueNums;
public int returnRandom() {
Random r = new Random();
int rand = r.nextInt(10000);
if(this.uniqueNums.capacity() != 0) {
for(int i = 0; i < this.uniqueNums.capacity(); i++) {
if(rand == this.uniqueNums.get(i))
returnRandom();
}
}
this.uniqueNums.add(rand);
return rand;
}