Java Code Examples for org.apache.commons.rng.UniformRandomProvider#nextLong()

The following examples show how to use org.apache.commons.rng.UniformRandomProvider#nextLong() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RNGUtils.java    From commons-rng with Apache License 2.0 6 votes vote down vote up
/**
 * Wrap the random generator with an {@link IntProvider} that will use the upper
 * 32-bits of the {@code long} from {@link UniformRandomProvider#nextLong()}.
 * The input must be a {@link RandomLongSource}.
 *
 * @param rng The random generator.
 * @return the upper bits random generator.
 * @throws ApplicationException If the input source native type is not 64-bit.
 */
static UniformRandomProvider createLongUpperBitsIntProvider(final UniformRandomProvider rng) {
    if (rng instanceof RandomLongSource) {
        return new IntProvider() {
            @Override
            public int next() {
                return (int) (rng.nextLong() >>> 32);
            }

            @Override
            public String toString() {
                return "Long upper-bits " + rng.toString();
            }
        };
    }
    throw new ApplicationException(NOT_LONG_SOURCE + rng);
}
 
Example 2
Source File: DoubleSplitPerformance.java    From commons-numbers with Apache License 2.0 6 votes vote down vote up
/**
 * Create the factors.
 */
@Setup
public void setup() {
    final UniformRandomProvider rng = RandomSource.create(RandomSource.XO_RO_SHI_RO_1024_PP);
    a = new double[size];
    for (int i = 0; i < size; i++) {
        long bits = rng.nextLong() & SIGN_MATISSA_MASK;
        // The exponent will either be small or big
        if (rng.nextDouble() < edge) {
            bits |= EXP_SMALL;
        } else {
            bits |= EXP_BIG;
        }
        a[i] = Double.longBitsToDouble(bits);
    }
}
 
Example 3
Source File: RNGUtils.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Combine the two random generators using a {@code xor} operations.
 * The input must be either a {@link RandomIntSource} or {@link RandomLongSource}.
 * The returned type will match the native output type of {@code rng1}.
 *
 * <pre>
 * {@code
 * rng1.nextInt() ^ rng2.nextInt()
 * }
 * </pre>
 *
 * @param rng1 The first random generator.
 * @param rng2 The second random generator.
 * @return the combined random generator.
 * @throws ApplicationException If the input source native type is not recognised.
 */
static UniformRandomProvider createXorProvider(final UniformRandomProvider rng1,
    final UniformRandomProvider rng2) {
    if (rng1 instanceof RandomIntSource) {
        return new IntProvider() {
            @Override
            public int next() {
                return rng1.nextInt() ^ rng2.nextInt();
            }

            @Override
            public String toString() {
                return rng1.toString() + XOR + rng2.toString();
            }
        };
    }
    if (rng1 instanceof RandomLongSource) {
        return new LongProvider() {
            @Override
            public long next() {
                return rng1.nextLong() ^ rng2.nextLong();
            }

            @Override
            public String toString() {
                return rng1.toString() + XOR + rng2.toString();
            }
        };
    }
    throw new ApplicationException(UNRECOGNISED_NATIVE_TYPE + rng1);
}
 
Example 4
Source File: SeedArrayGenerationPerformance.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Fill the array between {@code start} inclusive and {@code end} exclusive from the RNG.
 * The lock is used to guard access to the generator.
 *
 * @param lock Lock guarding access to the generator.
 * @param rng Random generator.
 * @param array Array data.
 * @param start Start (inclusive).
 * @param end End (exclusive).
 */
private static void nextLong(Lock lock, UniformRandomProvider rng, long[] array, int start, int end) {
    lock.lock();
    try {
        for (int i = start; i < end; i++) {
            array[i] = rng.nextLong();
        }
    } finally {
        lock.unlock();
    }
}
 
Example 5
Source File: SeedArrayGenerationPerformance.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Get the next {@code long} from the RNG. The lock is used to guard access to the generator.
 *
 * @param lock Lock guarding access to the generator.
 * @param rng Random generator.
 * @return the long
 */
private static long nextLong(Lock lock, UniformRandomProvider rng) {
    lock.lock();
    try {
        return rng.nextLong();
    } finally {
        lock.unlock();
    }
}
 
Example 6
Source File: ComplexPerformance.java    From commons-numbers with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a random double number with a random sign and mantissa and a large range for
 * the exponent. The numbers will not be uniform over the range. This samples randomly
 * using the components of a double. The limiting distribution is the log-uniform distribution.
 *
 * @param rng Random number generator.
 * @return the random number
 * @see <a href="https://en.wikipedia.org/wiki/Reciprocal_distribution">Reciprocal (log-uniform) distribution</a>
 */
private static double createLogUniformNumber(UniformRandomProvider rng) {
    // Create random doubles using random bits in the sign bit and the mantissa.
    // Then create an exponent in the range -64 to 64. Thus the sum product
    // of 4 max or min values will not over or underflow.
    final long mask = ((1L << 52) - 1) | 1L << 63;
    final long bits = rng.nextLong() & mask;
    // The exponent must be unsigned so + 1023 to the signed exponent
    final long exp = rng.nextInt(129) - 64 + 1023;
    return Double.longBitsToDouble(bits | (exp << 52));
}
 
Example 7
Source File: ThreadLocalPerformance.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * @param sources Source of randomness.
 * @return the result
 */
@Benchmark
@Threads(4)
public long threadLocalRandomSourceCurrent(Sources sources) {
    final UniformRandomProvider rng = ThreadLocalRandomSource.current(sources.getRandomSource());
    long result = 0;
    for (int i = 0; i < numValues; i++) {
        result = result ^ rng.nextLong();
    }
    return result;
}
 
Example 8
Source File: ThreadLocalPerformance.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * @param sources Source of randomness.
 * @return the result
 */
@Benchmark
@Threads(4)
public long randomSourceCreate(Sources sources) {
    final UniformRandomProvider rng = RandomSource.create(sources.getRandomSource());
    long result = 0;
    for (int i = 0; i < numValues; i++) {
        result = result ^ rng.nextLong();
    }
    return result;
}
 
Example 9
Source File: SeedGenerationPerformance.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Get the next {@code long} from the RNG. The lock is used to guard access to the generator.
 *
 * @param lock Lock guarding access to the generator.
 * @param rng Random generator.
 * @return the long
 */
private static long nextLong(Lock lock, UniformRandomProvider rng) {
    lock.lock();
    try {
        return rng.nextLong();
    } finally {
        lock.unlock();
    }
}
 
Example 10
Source File: VectorPerformance.java    From commons-geometry with Apache License 2.0 5 votes vote down vote up
/** Creates a random double number with a random sign and mantissa and a large range for
 * the exponent. The numbers will not be uniform over the range.
 * @param rng random number generator
 * @return the random number
 */
private static double createRandomDouble(final UniformRandomProvider rng) {
    // Create random doubles using random bits in the sign bit and the mantissa.
    // Then create an exponent in the range -64 to 64. Thus the sum product
    // of 4 max or min values will not over or underflow.
    final long mask = ((1L << 52) - 1) | 1L << 63;
    final long bits = rng.nextLong() & mask;
    // The exponent must be unsigned so + 1023 to the signed exponent
    final long exp = rng.nextInt(129) - 64 + 1023;
    return Double.longBitsToDouble(bits | (exp << 52));
}
 
Example 11
Source File: RNGUtils.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Wrap the random generator with a new instance that will combine the bits
 * using a {@code xor} operation with the output from {@link ThreadLocalRandom}.
 * The input must be either a {@link RandomIntSource} or {@link RandomLongSource}.
 *
 * <pre>
 * {@code
 * ThreadLocalRandom.current().nextInt() ^ rng.nextInt()
 * }
 * </pre>
 *
 * @param rng The random generator.
 * @return the combined random generator.
 * @throws ApplicationException If the input source native type is not recognised.
 */
static UniformRandomProvider createThreadLocalRandomProvider(final UniformRandomProvider rng) {
    if (rng instanceof RandomIntSource) {
        return new IntProvider() {
            @Override
            public int next() {
                return ThreadLocalRandom.current().nextInt() ^ rng.nextInt();
            }

            @Override
            public String toString() {
                return TLR_MIXED + rng.toString();
            }
        };
    }
    if (rng instanceof RandomLongSource) {
        return new LongProvider() {
            @Override
            public long next() {
                return ThreadLocalRandom.current().nextLong() ^ rng.nextLong();
            }

            @Override
            public String toString() {
                return TLR_MIXED + rng.toString();
            }
        };
    }
    throw new ApplicationException(UNRECOGNISED_NATIVE_TYPE + rng);
}
 
Example 12
Source File: RNGUtils.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Wrap the random generator with a new instance that will combine the bits
 * using a {@code xor} operation with a generated hash code. The input must be either
 * a {@link RandomIntSource} or {@link RandomLongSource}.
 *
 * <pre>
 * {@code
 * System.identityHashCode(new Object()) ^ rng.nextInt()
 * }
 * </pre>
 *
 * Note: This generator will be slow.
 *
 * @param rng The random generator.
 * @return the combined random generator.
 * @throws ApplicationException If the input source native type is not recognised.
 * @see System#identityHashCode(Object)
 */
static UniformRandomProvider createHashCodeProvider(final UniformRandomProvider rng) {
    if (rng instanceof RandomIntSource) {
        return new IntProvider() {
            @Override
            public int next() {
                return System.identityHashCode(new Object()) ^ rng.nextInt();
            }

            @Override
            public String toString() {
                return HASH_CODE + rng.toString();
            }
        };
    }
    if (rng instanceof RandomLongSource) {
        return new LongProvider() {
            @Override
            public long next() {
                final long mix = NumberFactory.makeLong(System.identityHashCode(new Object()),
                                                        System.identityHashCode(new Object()));
                return mix ^ rng.nextLong();
            }

            @Override
            public String toString() {
                return HASH_CODE + rng.toString();
            }
        };
    }
    throw new ApplicationException(UNRECOGNISED_NATIVE_TYPE + rng);
}
 
Example 13
Source File: SeedArrayGenerationPerformance.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * @param sources Source of randomness.
 * @param sizes Size of the seed.
 * @return the seed
 */
@Benchmark
public long[] createLongArraySeed(SeedRandomSources sources, SeedSizes sizes) {
    final UniformRandomProvider rng = sources.getGenerator();
    final long[] seed = new long[sizes.getSize()];
    for (int i = 0; i < seed.length; i++) {
        seed[i] = rng.nextLong();
    }
    return seed;
}
 
Example 14
Source File: ProviderBuilder.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the full length seed array from the input seed using the method
 * recommended for the generator. This is a high quality Weyl increment composed
 * of a hex character permutation.
 *
 * @param source Source of randomness.
 * @return the seed array
 */
private long[] createMswsSeed(UniformRandomProvider source) {
    final long increment = SeedUtils.createLongHexPermutation(source);
    // The initial state should not be low complexity but the Weyl
    // state can be any number.
    final long state = increment;
    final long weylState = source.nextLong();
    return new long[] {state, weylState, increment};
}
 
Example 15
Source File: RandomAssert.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Assert that following a set number of warm-up cycles the random generator produces
 * at least one non-zero output for {@link UniformRandomProvider#nextLong()} over the
 * given number of test cycles. This is used to test a poorly seeded generator can recover
 * internal state to generate "random" output.
 *
 * @param rng Random generator.
 * @param warmupCycles Number of warm-up cycles.
 * @param testCycles Number of test cycles.
 */
public static void assertNextLongNonZeroOutput(UniformRandomProvider rng,
                                               int warmupCycles, int testCycles) {
    for (int i = 0; i < warmupCycles; i++) {
        rng.nextLong();
    }
    for (int i = 0; i < testCycles; i++) {
        if (rng.nextLong() != 0L) {
            return;
        }
    }
    Assert.fail("No non-zero output after " + (warmupCycles + testCycles) + " cycles");
}
 
Example 16
Source File: RandomAssert.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Assert that the random generator produces a <strong>different</strong> output using
 * {@link UniformRandomProvider#nextLong()} to the expected output.
 *
 * @param expected Expected output.
 * @param rng Random generator.
 */
public static void assertNotEquals(long[] expected, UniformRandomProvider rng) {
    for (int i = 0; i < expected.length; i++) {
        if (expected[i] != rng.nextLong()) {
            return;
        }
    }
    Assert.fail("Expected a different nextLong output");
}
 
Example 17
Source File: SeedArrayGenerationPerformance.java    From commons-rng with Apache License 2.0 3 votes vote down vote up
/**
 * Fill the array between {@code start} inclusive and {@code end} exclusive from the
 * RNG. This is synchronized on the generator.
 *
 * @param rng Random generator.
 * @param array Array data.
 * @param start Start (inclusive).
 * @param end End (exclusive).
 */
private static void nextLong(UniformRandomProvider rng, long[] array, int start, int end) {
    synchronized (rng) {
        for (int i = start; i < end; i++) {
            array[i] = rng.nextLong();
        }
    }
}
 
Example 18
Source File: SeedGenerationPerformance.java    From commons-rng with Apache License 2.0 2 votes vote down vote up
/**
 * Get the next {@code long} from the RNG. This is synchronized on the generator.
 *
 * @param rng Random generator.
 * @return the long
 */
private static long nextLong(UniformRandomProvider rng) {
    synchronized (rng) {
        return rng.nextLong();
    }
}
 
Example 19
Source File: StickySumPerformance.java    From commons-numbers with Apache License 2.0 2 votes vote down vote up
/**
 * Create the next double in the range [1, 2). All mantissa bits have an equal
 * probability of being set.
 *
 * @param rng Generator of random numbers.
 * @return the double
 */
private static double nextDouble(UniformRandomProvider rng) {
    final long bits = rng.nextLong() & SIGN_MATISSA_MASK;
    return Double.longBitsToDouble(bits | EXP);
}
 
Example 20
Source File: SeedArrayGenerationPerformance.java    From commons-rng with Apache License 2.0 2 votes vote down vote up
/**
 * Get the next {@code long} from the RNG. This is synchronized on the generator.
 *
 * @param rng Random generator.
 * @return the long
 */
private static long nextLong(UniformRandomProvider rng) {
    synchronized (rng) {
        return rng.nextLong();
    }
}