Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //given : generate N unique integers, return array
- // N - any positive int 32
- public int [] randomize(int n){
- Set<Integer> memo = new HashSet<>();
- int [] result = new int[n];
- for(int i = 0; i < n; i++){
- int candidate = ThreadLocalRandom.current().nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE); // random int 32 from java library
- while(memo.contains(candidate)){
- candidate = ThreadLocalRandom.current().nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE); //
- }
- memo.add(candidate);
- reuslt[i] = candidate;
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment