Created
November 11, 2017 02:24
-
-
Save iUmarov/41ea9a882c412427662175170c8b226d to your computer and use it in GitHub Desktop.
Unique Random Int Numbers in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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