Java ThreadLocalRandom randomLongsNonDuplicates(final long count, final long min, final long max)

Here you can find the source of randomLongsNonDuplicates(final long count, final long min, final long max)

Description

random Longs Non Duplicates

License

Apache License

Declaration

public static long[] randomLongsNonDuplicates(final long count, final long min, final long max) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;

public class Main {
    public static long[] randomLongsNonDuplicates(final long count, final long min, final long max) {
        ThreadLocalRandom rng = ThreadLocalRandom.current();
        // Note: use LinkedHashSet to maintain insertion order
        //TODO not efficient
        Set<Long> generated = new LinkedHashSet<>();
        while (generated.size() < count) {
            Long next = randLong(rng, min, max);
            generated.add(next);//from   ww w  .j  ava2s .  c om
        }
        return generated.stream().mapToLong(i -> i).toArray();
    }

    private static long randLong(Random random, long min, long max) {
        //TODO swith impl
        return min + ((long) (random.nextDouble() * (max + 1 - min)));
    }
}

Related

  1. randomInt()
  2. randomInt(int bound)
  3. randomInteger(int min, int max)
  4. randomIntegerList(int min, int max, int minLength, int maxLength)
  5. randomLongs(final long count, final long min, final long max)
  6. randomNum(int min, int max)
  7. randomNums(int min, int max, int count)
  8. randomPer(long num, long divisor)
  9. randomReal(float range)