Skip to content

Instantly share code, notes, and snippets.

@iUmarov
Created November 11, 2017 02:24
Show Gist options
  • Save iUmarov/41ea9a882c412427662175170c8b226d to your computer and use it in GitHub Desktop.
Save iUmarov/41ea9a882c412427662175170c8b226d to your computer and use it in GitHub Desktop.
Unique Random Int Numbers in Java
public class UniqueIntRandom {
private ArrayList<Integer> numberStore;
public UniqueIntRandom(int startingNum, int endNum) {
numberStore = new ArrayList<>();
initRandomNumbers(startingNum, endNum);
}
public UniqueIntRandom(int endNum) {
numberStore = new ArrayList<>();
initRandomNumbers(1, endNum);
}
private void initRandomNumbers(int startingNum, int endNum) {
for (int i = startingNum; i <= endNum; i++) {
numberStore.add(i);
}
Collections.shuffle(numberStore);
}
public int nextUniqueRandom() throws IndexOutOfBoundsException {
int temp = numberStore.get(0);
numberStore.remove(0);
return temp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment