Java Code Examples for org.apache.commons.rng.UniformRandomProvider#nextInt()
The following examples show how to use
org.apache.commons.rng.UniformRandomProvider#nextInt() .
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: VectorPerformance.java From commons-geometry with Apache License 2.0 | 6 votes |
/** Create a supplier that produces doubles of the given type. * @param type type of doubles to produce * @param rng random provider * @return a supplier that produces doubles of the given type */ private DoubleSupplier createDoubleSupplier(final String type, final UniformRandomProvider rng) { switch (type) { case RANDOM: return () -> createRandomDouble(rng); case NORMALIZABLE: final ZigguratNormalizedGaussianSampler sampler = ZigguratNormalizedGaussianSampler.of(rng); return () -> { double n = sampler.sample(); return n == 0 ? 0.1 : n; // do not return exactly zero }; case EDGE: return () -> EDGE_NUMBERS[rng.nextInt(EDGE_NUMBERS.length)]; default: throw new IllegalStateException("Invalid input type: " + type); } }
Example 2
Source File: WelzlEncloser3DTest.java From commons-geometry with Apache License 2.0 | 6 votes |
@Test public void testLargeSamples() { // arrange final UniformRandomProvider random = RandomSource.create(RandomSource.WELL_1024_A, 0x35ddecfc78131e1dL); final UnitSphereSampler sr = new UnitSphereSampler(3, random); for (int k = 0; k < 50; ++k) { // define the reference sphere we want to compute double d = 25 * random.nextDouble(); double refRadius = 10 * random.nextDouble(); Vector3D refCenter = Vector3D.linearCombination(d, Vector3D.of(sr.nextVector())); // set up a large sample inside the reference sphere int nbPoints = random.nextInt(1000); List<Vector3D> points = new ArrayList<>(); for (int i = 0; i < nbPoints; ++i) { double r = refRadius * random.nextDouble(); points.add(Vector3D.linearCombination(1.0, refCenter, r, Vector3D.of(sr.nextVector()))); } // act/assert // test we find a sphere at most as large as the one used for random drawings checkSphere(points, refRadius); } }
Example 3
Source File: WelzlEncloser2DTest.java From commons-geometry with Apache License 2.0 | 6 votes |
@Test public void testLargeSamples() { // arrange UniformRandomProvider random = RandomSource.create(RandomSource.WELL_1024_A, 0xa2a63cad12c01fb2L); for (int k = 0; k < 100; ++k) { int nbPoints = random.nextInt(10000); List<Vector2D> points = new ArrayList<>(); for (int i = 0; i < nbPoints; ++i) { double x = random.nextDouble(); double y = random.nextDouble(); points.add(Vector2D.of(x, y)); } // act/assert checkDisk(points); } }
Example 4
Source File: RNGUtils.java From commons-rng with Apache License 2.0 | 6 votes |
/** * Wrap the random generator with an {@link IntProvider} that will use * {@link UniformRandomProvider#nextInt()}. * An input {@link RandomIntSource} is returned unmodified. * * @param rng The random generator. * @return the int random generator. */ static UniformRandomProvider createIntProvider(final UniformRandomProvider rng) { if (!(rng instanceof RandomIntSource)) { return new IntProvider() { @Override public int next() { return rng.nextInt(); } @Override public String toString() { return "Int bits " + rng.toString(); } }; } return rng; }
Example 5
Source File: VectorPerformance.java From commons-geometry with Apache License 2.0 | 5 votes |
/** 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 6
Source File: DoubleSplitPerformance.java From commons-numbers with Apache License 2.0 | 5 votes |
/** * 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++) { // Value in (-1, 1) double value = rng.nextDouble() * (rng.nextBoolean() ? -1 : 1); // The number will either be small or non-normal if (rng.nextDouble() < edge) { value *= NON_NORMAL[rng.nextInt(NON_NORMAL.length)]; } a[i] = value; } }
Example 7
Source File: ComplexPerformance.java From commons-numbers with Apache License 2.0 | 5 votes |
/** * 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 8
Source File: SeedArrayGenerationPerformance.java From commons-rng with Apache License 2.0 | 5 votes |
/** * @param sources Source of randomness. * @param sizes Size of the seed. * @return the seed */ @Benchmark public int[] createIntArraySeed(SeedRandomSources sources, SeedSizes sizes) { final UniformRandomProvider rng = sources.getGenerator(); final int[] seed = new int[sizes.getSize()]; for (int i = 0; i < seed.length; i++) { seed[i] = rng.nextInt(); } return seed; }
Example 9
Source File: SeedArrayGenerationPerformance.java From commons-rng with Apache License 2.0 | 5 votes |
/** * 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 nextInt(Lock lock, UniformRandomProvider rng, int[] array, int start, int end) { lock.lock(); try { for (int i = start; i < end; i++) { array[i] = rng.nextInt(); } } finally { lock.unlock(); } }
Example 10
Source File: SeedArrayGenerationPerformance.java From commons-rng with Apache License 2.0 | 5 votes |
/** * Get the next {@code int} 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 int */ private static int nextInt(Lock lock, UniformRandomProvider rng) { lock.lock(); try { return rng.nextInt(); } finally { lock.unlock(); } }
Example 11
Source File: SeedGenerationPerformance.java From commons-rng with Apache License 2.0 | 5 votes |
/** * Get the next {@code int} 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 int */ private static int nextInt(Lock lock, UniformRandomProvider rng) { lock.lock(); try { return rng.nextInt(); } finally { lock.unlock(); } }
Example 12
Source File: RNGUtils.java From commons-rng with Apache License 2.0 | 5 votes |
/** * 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 13
Source File: RNGUtils.java From commons-rng with Apache License 2.0 | 5 votes |
/** * 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 14
Source File: ComplexPerformance.java From commons-numbers with Apache License 2.0 | 5 votes |
/** * Creates a random double number with a random sign and uniform range. * * @param rng Random number generator. * @return the random number */ private static double createUniformNumber(UniformRandomProvider rng) { // Note: [0, 1) - 1 is [-1, 0). // Since the 1 is a 50/50 sample the result is the interval [-1, 1) // using the 2^54 dyadic rationals in the interval. // The range is not critical. The numbers will have approximately 50% // with the same exponent, max, matching that of RANGE and the rest smaller // exponents down to (max - 53) since the uniform deviate is limited to 2^-53. return (rng.nextDouble() - rng.nextInt(1)) * RANGE; }
Example 15
Source File: ProvidersCommonParametricTest.java From commons-rng with Apache License 2.0 | 5 votes |
/** * Tests uniformity of the distribution produced by {@code nextInt(int)}. * * @param rng Generator. * @param max Upper bound. * @param sampleSize Number of random values generated. */ private void checkNextIntegerInRange(final UniformRandomProvider rng, final int max, int sampleSize) { final Callable<Integer> nextMethod = new Callable<Integer>() { @Override public Integer call() throws Exception { return rng.nextInt(max); } }; checkNextInRange(max, sampleSize, nextMethod); }
Example 16
Source File: SeedArrayGenerationPerformance.java From commons-rng with Apache License 2.0 | 3 votes |
/** * 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 nextInt(UniformRandomProvider rng, int[] array, int start, int end) { synchronized (rng) { for (int i = start; i < end; i++) { array[i] = rng.nextInt(); } } }
Example 17
Source File: ComplexPerformance.java From commons-numbers with Apache License 2.0 | 2 votes |
/** * Creates a random double number that will be an edge case: * {@code +/-inf, +/-max, +/-min, +/-0, nan}. * * @param rng Random number generator. * @return the random number */ private static double createEdgeNumber(UniformRandomProvider rng) { return EDGE_NUMBERS[rng.nextInt(EDGE_NUMBERS.length)]; }
Example 18
Source File: SeedArrayGenerationPerformance.java From commons-rng with Apache License 2.0 | 2 votes |
/** * Get the next {@code int} from the RNG. This is synchronized on the generator. * * @param rng Random generator. * @return the int */ private static int nextInt(UniformRandomProvider rng) { synchronized (rng) { return rng.nextInt(); } }
Example 19
Source File: LinearCombinationUtils.java From commons-numbers with Apache License 2.0 | 2 votes |
/** * Create a double in the range [-1, 1). * * @param rng source of randomness * @return the double */ private static double m1p1(UniformRandomProvider rng) { // Create in the range [0, 1) then randomly subtract 1. // This samples the 2^54 dyadic rationals in the range. return rng.nextDouble() - rng.nextInt(1); }
Example 20
Source File: CStandardTest.java From commons-numbers with Apache License 2.0 | 2 votes |
/** * Create a number in the range {@code [-5,5)}. * * @param rng the random generator * @return the number */ private static double next(UniformRandomProvider rng) { // Note: [0, 1) minus 1 is [-1, 0). This occurs half the time to create [-1, 1). return (rng.nextDouble() - rng.nextInt(1)) * 5; }