public static UniformLongSampler of()

in commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/UniformLongSampler.java [295:332]


    public static UniformLongSampler of(UniformRandomProvider rng,
                                        long lower,
                                        long upper) {
        if (lower > upper) {
            throw new IllegalArgumentException(lower  + " > " + upper);
        }

        // Choose the algorithm depending on the range

        // Edge case for no range.
        // This must be done first as the methods to handle lower == 0
        // do not handle upper == 0.
        if (upper == lower) {
            return new FixedUniformLongSampler(lower);
        }

        // Algorithms to ignore the lower bound if it is zero.
        if (lower == 0) {
            return createZeroBoundedSampler(rng, upper);
        }

        final long range = (upper - lower) + 1;
        // Check power of 2 first to handle range == 2^63.
        if (isPowerOf2(range)) {
            return new OffsetUniformLongSampler(lower,
                                                new PowerOf2RangeUniformLongSampler(rng, range));
        }
        if (range <= 0) {
            // The range is too wide to fit in a positive long (larger
            // than 2^63); use a simple rejection method.
            // Note: if range == 0 then the input is [Long.MIN_VALUE, Long.MAX_VALUE].
            // No specialisation exists for this and it is handled as a large range.
            return new LargeRangeUniformLongSampler(rng, lower, upper);
        }
        // Use a sample from the range added to the lower bound.
        return new OffsetUniformLongSampler(lower,
                                            new SmallRangeUniformLongSampler(rng, range));
    }