Skip to content

Instantly share code, notes, and snippets.

@hungbang
Forked from Bharathbrothers/Unique.java
Created June 10, 2021 16:24
Show Gist options
  • Save hungbang/5bdab2dc182711346ed305f2a0403936 to your computer and use it in GitHub Desktop.
Save hungbang/5bdab2dc182711346ed305f2a0403936 to your computer and use it in GitHub Desktop.
How to generate unique random numbers in Java?
ArrayList<Integer> array = new ArrayList<>(4);
ArrayList<Integer> input = new ArrayList<>(4);
for(int i=0;i<10;i++){
array.add(i);
}
Collections.shuffle(array);
for(int j=0;j<4;j++){
input.add(array.get(j));
}
// This generates unique numbers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment