Java Code Examples for org.apache.commons.rng.UniformRandomProvider#nextDouble()
The following examples show how to use
org.apache.commons.rng.UniformRandomProvider#nextDouble() .
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: Vector3DTest.java From commons-geometry with Apache License 2.0 | 6 votes |
@Test public void testDotProduct_accuracy() { // we compare accurate versus naive dot product implementations // on regular vectors (i.e. not extreme cases like in the previous test) UniformRandomProvider random = RandomSource.create(RandomSource.WELL_1024_A, 553267312521321237L); for (int i = 0; i < 10000; ++i) { // arrange double ux = 10000 * random.nextDouble(); double uy = 10000 * random.nextDouble(); double uz = 10000 * random.nextDouble(); double vx = 10000 * random.nextDouble(); double vy = 10000 * random.nextDouble(); double vz = 10000 * random.nextDouble(); // act double sNaive = ux * vx + uy * vy + uz * vz; double sAccurate = Vector3D.of(ux, uy, uz).dot(Vector3D.of(vx, vy, vz)); // assert Assert.assertEquals(sNaive, sAccurate, 2.5e-16 * sAccurate); } }
Example 2
Source File: DoubleSplitPerformance.java From commons-numbers with Apache License 2.0 | 6 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++) { 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: DiskGeneratorTest.java From commons-geometry with Apache License 2.0 | 6 votes |
@Test public void testRandom() { // arrange final UniformRandomProvider random = RandomSource.create(RandomSource.WELL_1024_A, 0x12faa818373ffe90L); final UnitSphereSampler sr = new UnitSphereSampler(2, random); for (int i = 0; i < 500; ++i) { double d = 25 * random.nextDouble(); double refRadius = 10 * random.nextDouble(); Vector2D refCenter = Vector2D.linearCombination(d, Vector2D.of(sr.nextVector())); List<Vector2D> support = new ArrayList<>(); for (int j = 0; j < 3; ++j) { support.add(Vector2D.linearCombination(1.0, refCenter, refRadius, Vector2D.of(sr.nextVector()))); } // act EnclosingBall<Vector2D> disk = generator.ballOnSupport(support); // assert Assert.assertEquals(0.0, refCenter.distance(disk.getCenter()), 3e-9 * refRadius); Assert.assertEquals(refRadius, disk.getRadius(), 7e-10 * refRadius); } }
Example 4
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 5
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 6
Source File: SphereGeneratorTest.java From commons-geometry with Apache License 2.0 | 6 votes |
@Test public void testRandom() { // arrange final UniformRandomProvider random = RandomSource.create(RandomSource.WELL_1024_A, 0xd015982e9f31ee04L); final UnitSphereSampler sr = new UnitSphereSampler(3, random); for (int i = 0; i < 100; ++i) { double d = 25 * random.nextDouble(); double refRadius = 10 * random.nextDouble(); Vector3D refCenter = Vector3D.linearCombination(d, Vector3D.of(sr.nextVector())); List<Vector3D> support = new ArrayList<>(); for (int j = 0; j < 5; ++j) { support.add(Vector3D.linearCombination(1.0, refCenter, refRadius, Vector3D.of(sr.nextVector()))); } // act EnclosingBall<Vector3D> sphere = generator.ballOnSupport(support); // assert Assert.assertEquals(0.0, refCenter.distance(sphere.getCenter()), 4e-7 * refRadius); Assert.assertEquals(refRadius, sphere.getRadius(), 1e-7 * refRadius); } }
Example 7
Source File: QuaternionRotationTest.java From commons-geometry with Apache License 2.0 | 6 votes |
@Test public void testMultiply_numericalStability() { // arrange int slices = 1024; double delta = (8.0 * PlaneAngleRadians.PI / 3.0) / slices; QuaternionRotation q = QuaternionRotation.identity(); UniformRandomProvider rand = RandomSource.create(RandomSource.JDK, 2L); // act for (int i = 0; i < slices; ++i) { double angle = rand.nextDouble(); QuaternionRotation forward = QuaternionRotation.fromAxisAngle(PLUS_DIAGONAL, angle); QuaternionRotation backward = QuaternionRotation.fromAxisAngle(PLUS_DIAGONAL, delta - angle); q = q.multiply(forward).multiply(backward); } // assert Assert.assertTrue(q.getQuaternion().getW() > 0); Assert.assertEquals(1.0, q.getQuaternion().norm(), EPS); assertRotationEquals(StandardRotations.PLUS_DIAGONAL_TWO_THIRDS_PI, q); }
Example 8
Source File: Vector3DTest.java From commons-geometry with Apache License 2.0 | 6 votes |
@Test public void testCrossProduct_accuracy() { // we compare accurate versus naive cross product implementations // on regular vectors (i.e. not extreme cases like in the previous test) UniformRandomProvider random = RandomSource.create(RandomSource.WELL_1024_A, 885362227452043215L); for (int i = 0; i < 10000; ++i) { // arrange double ux = 10000 * random.nextDouble(); double uy = 10000 * random.nextDouble(); double uz = 10000 * random.nextDouble(); double vx = 10000 * random.nextDouble(); double vy = 10000 * random.nextDouble(); double vz = 10000 * random.nextDouble(); // act Vector3D cNaive = Vector3D.of(uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx); Vector3D cAccurate = Vector3D.of(ux, uy, uz).cross(Vector3D.of(vx, vy, vz)); // assert Assert.assertEquals(0.0, cAccurate.distance(cNaive), 6.0e-15 * cAccurate.norm()); } }
Example 9
Source File: SphereTest.java From commons-geometry with Apache License 2.0 | 5 votes |
@Test public void testToTree_randomSpheres() { // arrange UniformRandomProvider rand = RandomSource.create(RandomSource.XO_RO_SHI_RO_128_PP, 1L); DoublePrecisionContext precision = new EpsilonDoublePrecisionContext(1e-10); double min = 1e-1; double max = 1e2; DoubleSupplier randDouble = () -> (rand.nextDouble() * (max - min)) + min; int count = 10; for (int i = 0; i < count; ++i) { Vector3D center = Vector3D.of( randDouble.getAsDouble(), randDouble.getAsDouble(), randDouble.getAsDouble()); double radius = randDouble.getAsDouble(); Sphere sphere = Sphere.from(center, radius, precision); for (int s = 0; s < 7; ++s) { // act RegionBSPTree3D tree = sphere.toTree(s); // assert Assert.assertEquals((int)(8 * Math.pow(4, s)), tree.getBoundaries().size()); Assert.assertTrue(tree.isFinite()); Assert.assertFalse(tree.isEmpty()); Assert.assertTrue(tree.getSize() < sphere.getSize()); } } }
Example 10
Source File: MarsagliaTsangWangDiscreteSamplerTest.java From commons-rng with Apache License 2.0 | 5 votes |
/** * Test samples from a distribution expressed using {@code double} probabilities. */ @Test public void testRealProbabilityDistributionSamples() { // These do not have to sum to 1 final double[] probabilities = new double[11]; final UniformRandomProvider rng = RandomSource.create(RandomSource.SPLIT_MIX_64); for (int i = 0; i < probabilities.length; i++) { probabilities[i] = rng.nextDouble(); } // First test the table is completely filled to 2^30 final UniformRandomProvider dummyRng = new FixedSequenceIntProvider(new int[] {0xffffffff}); final SharedStateDiscreteSampler dummySampler = MarsagliaTsangWangDiscreteSampler.Enumerated.of(dummyRng, probabilities); // This will throw if the table is incomplete as it hits the upper limit dummySampler.sample(); // Do a test of the actual sampler final SharedStateDiscreteSampler sampler = MarsagliaTsangWangDiscreteSampler.Enumerated.of(rng, probabilities); final int numberOfSamples = 10000; final long[] samples = new long[probabilities.length]; for (int i = 0; i < numberOfSamples; i++) { samples[sampler.sample()]++; } final ChiSquareTest chiSquareTest = new ChiSquareTest(); // Pass if we cannot reject null hypothesis that the distributions are the same. Assert.assertFalse(chiSquareTest.chiSquareTest(probabilities, samples, 0.001)); }
Example 11
Source File: ComplexTest.java From commons-numbers with Apache License 2.0 | 5 votes |
@Test void testPolarConstructorAbsArg() { // The test should work with any seed but use a fixed seed to avoid build // instability. final UniformRandomProvider rng = RandomSource.create(RandomSource.SPLIT_MIX_64, 678678638L); for (int i = 0; i < 10; i++) { final double rho = rng.nextDouble(); // Range (pi, pi]: lower exclusive, upper inclusive final double theta = pi - rng.nextDouble() * 2 * pi; final Complex z = Complex.ofPolar(rho, theta); // Match within 1 ULP Assertions.assertEquals(rho, z.abs(), Math.ulp(rho)); Assertions.assertEquals(theta, z.arg(), Math.ulp(theta)); } }
Example 12
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 13
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 14
Source File: LinearCombinationsTest.java From commons-numbers with Apache License 2.0 | 5 votes |
/** * Test the sum of vectors composed of sub-vectors of [a1, a2, a3, a4] * [a1, a2, -a3, -a4] * where a1^2 + a2^2 = 1 and a3^2 + a4^2 = 1 such that the sum is approximately 0 every * 4 products. This is a test that is failed by various implementations that accumulate the * round-off sum in single or 2-fold precision. */ @Test void testSumZero() { // Fixed seed for stability final UniformRandomProvider rng = RandomSource.create(RandomSource.XO_RO_SHI_RO_128_PP, 876543L); final int size = 10; // Create random doublets of pairs of numbers that sum to 1 or -1. for (int length = 4; length <= 12; length += 4) { final double[] a = new double[length]; final double[] b = new double[length]; for (int i = 0; i < size; i++) { // Flip-flop above and below zero double sign = 1; for (int k = 0; k < length; k += 4) { // Create 2 complex cis numbers final double theta1 = rng.nextDouble() * Math.PI / 2; final double theta2 = rng.nextDouble() * Math.PI / 2; a[k + 0] = b[k + 0] = Math.cos(theta1); a[k + 1] = b[k + 1] = Math.sin(theta1); a[k + 2] = b[k + 2] = Math.cos(theta2); a[k + 3] = b[k + 3] = Math.sin(theta2); a[k + 0] *= sign; a[k + 1] *= sign; a[k + 2] *= sign; a[k + 3] *= sign; // Invert second pair. // The sum of the pairs should be zero +/- floating point error. a[k + 2] = -a[k + 2]; a[k + 3] = -a[k + 3]; sign = -sign; } assertValue(LinearCombinations.DotK.DOT_3.value(a, b), a, b); } } }
Example 15
Source File: LinearCombinationTest.java From commons-numbers with Apache License 2.0 | 5 votes |
@Test void testArrayVsInline() { final UniformRandomProvider rng = RandomSource.create(RandomSource.XO_SHI_RO_256_PP); double sInline; double sArray; final double scale = 1e17; for (int i = 0; i < 10000; ++i) { final double u1 = scale * rng.nextDouble(); final double u2 = scale * rng.nextDouble(); final double u3 = scale * rng.nextDouble(); final double u4 = scale * rng.nextDouble(); final double v1 = scale * rng.nextDouble(); final double v2 = scale * rng.nextDouble(); final double v3 = scale * rng.nextDouble(); final double v4 = scale * rng.nextDouble(); // One sum. sInline = LinearCombination.value(u1, v1, u2, v2); sArray = LinearCombination.value(new double[] {u1, u2}, new double[] {v1, v2}); Assertions.assertEquals(sInline, sArray, 0); // Two sums. sInline = LinearCombination.value(u1, v1, u2, v2, u3, v3); sArray = LinearCombination.value(new double[] {u1, u2, u3}, new double[] {v1, v2, v3}); Assertions.assertEquals(sInline, sArray, 0); // Three sums. sInline = LinearCombination.value(u1, v1, u2, v2, u3, v3, u4, v4); sArray = LinearCombination.value(new double[] {u1, u2, u3, u4}, new double[] {v1, v2, v3, v4}); Assertions.assertEquals(sInline, sArray, 0); } }
Example 16
Source File: SpherePerformance.java From commons-geometry with Apache License 2.0 | 2 votes |
/** Return a random double bounded by {@link #MAX_VALUE} and {@link #MIN_VALUE}. * @param rand random provider * @return a random double value */ private static double nextDouble(final UniformRandomProvider rand) { return (rand.nextDouble() * (MAX_VALUE - MIN_VALUE)) + MIN_VALUE; }
Example 17
Source File: CirclePerformance.java From commons-geometry with Apache License 2.0 | 2 votes |
/** Return a random double bounded by {@link #MAX_VALUE} and {@link #MIN_VALUE}. * @param rand random provider * @return a random double value */ private static double nextDouble(final UniformRandomProvider rand) { return (rand.nextDouble() * (MAX_VALUE - MIN_VALUE)) + MIN_VALUE; }
Example 18
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; }
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); }