Example usage for org.apache.commons.math3.random RandomGenerator nextLong

List of usage examples for org.apache.commons.math3.random RandomGenerator nextLong

Introduction

In this page you can find the example usage for org.apache.commons.math3.random RandomGenerator nextLong.

Prototype

long nextLong();

Source Link

Document

Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.

Usage

From source file:com.analog.lyric.math.DimpleRandom.java

/**
 * Construct using specified underlying random generator and a randomly generated seed.
 * <p>/*from w  w w .  j  a va2 s  . com*/
 * The random value used to seed the generator can be obtained via {@link #getSeed()}.
 * <p>
 * @since 0.08
 */
public DimpleRandom(RandomGenerator randomGenerator) {
    this(randomGenerator, randomGenerator.nextLong());
}

From source file:com.github.rinde.rinsim.util.StochasticSuppliersTest.java

/**
 * Tests whether the rescaling of the mean of a truncated normal distribution
 * is implemented correctly.//from   www.  j av a  2s .  c  om
 */
@Test
public void testNormalScaleMean() {
    final double[] means = new double[] { 1d, 2d, 3d, 10d, 100d };
    final double[] sds = new double[] { 1d, 1d, 3d, 5d, 100d };

    for (int i = 0; i < means.length; i++) {
        final StochasticSupplier<Double> ss = StochasticSuppliers.normal().mean(means[i]).std(sds[i])
                .lowerBound(0).scaleMean().redrawWhenOutOfBounds().buildDouble();

        final RandomGenerator rng = new MersenneTwister(123);
        final SummaryStatistics stats = new SummaryStatistics();
        for (int j = 0; j < 10000; j++) {
            stats.addValue(ss.get(rng.nextLong()));
        }
        // 1 % deviation from mean is acceptable
        final double allowedDeviation = 0.01 * means[i];
        assertEquals(means[i], stats.getMean(), allowedDeviation);
    }

}

From source file:com.github.rinde.rinsim.scenario.generator.PoissonProcessTest.java

/**
 * Tests whether the Poisson process has a Poisson distribution.
 *//*w w  w .ja  v  a  2s .c om*/
@Test
public void testPoissonDistribution() {
    final double length = poisson.getLength();
    final Frequency f = new Frequency();

    final RandomGenerator rng = new MersenneTwister(0);
    for (int i = 0; i < 1000; i++) {

        final List<Double> doubles = poisson.generate(rng.nextLong());
        final List<Long> list = newArrayList();
        for (final double d : doubles) {
            list.add(DoubleMath.roundToLong(d, RoundingMode.HALF_UP));
        }
        ascendingOrderTest(list);
        // add the number of announcements
        f.addValue(list.size());
    }
    final double averageIntensity;
    if (poisson instanceof NonHomogenous && ((NonHomogenous) poisson).lambd instanceof SineIntensity) {
        final SineIntensity si = (SineIntensity) ((NonHomogenous) poisson).lambd;

        final double period = 1d / si.getFrequency();
        final double periods = length / period;
        final double totalEvents = si.area() * periods;
        averageIntensity = totalEvents / length;
    } else {
        averageIntensity = poisson.intensity;
    }
    assertTrue(isPoissonProcess(f, averageIntensity, length, 0.001));
    assertFalse(isPoissonProcess(f, 2, length, 0.01));
    assertFalse(isPoissonProcess(f, 0.1, length, 0.0001));
    assertFalse(isPoissonProcess(f, 15, length, 0.001));
    assertFalse(isPoissonProcess(f, 1000, length, 0.0001));
}

From source file:com.github.rinde.rinsim.central.SolverSimTest.java

/**
 * Tests whether the simulator produces the same objective value as the
 * solver.//from  w  ww.jav  a 2  s .co m
 * @throws IOException In case file loading fails.
 */
@Test
public void testOffline() throws IOException {
    final Gendreau06Scenario scenario = Gendreau06Parser.parser().addFile(ScenarioPaths.GENDREAU).offline()
            .parse().get(0);

    final RandomGenerator rng = new MersenneTwister(123);
    for (int i = 0; i < 5; i++) {
        final long seed = rng.nextLong();
        final DebugSolverCreator dsc = new DebugSolverCreator(seed, SI.MILLI(SI.SECOND));
        final Gendreau06ObjectiveFunction obj = Gendreau06ObjectiveFunction.instance();
        final ExperimentResults results = Experiment.builder()
                .addConfiguration(Central.solverConfiguration(dsc)).addScenario(scenario)
                .usePostProcessor(PostProcessors.statisticsPostProcessor(obj)).perform();
        assertEquals(1, results.getResults().size());
        assertEquals(1, dsc.arraysSolver.getInputs().size());
        assertEquals(1, dsc.arraysSolver.getOutputs().size());

        final SolutionObject[] sols = dsc.arraysSolver.getOutputs().get(0);
        int objVal = 0;
        for (final SolutionObject sol : sols) {
            objVal += sol.objectiveValue;
        }

        // convert the objective values computed by the solver to the unit of the
        // gendreau benchmark (minutes).
        final UnitConverter converter = SI.MILLI(SI.SECOND).getConverterTo(NonSI.MINUTE);
        final double objValInMinutes = converter.convert(objVal);

        final GlobalStateObject solverInput = dsc.solver.getInputs().get(0);
        final ImmutableList<ImmutableList<Parcel>> solverOutput = dsc.solver.getOutputs().get(0);

        assertEquals(obj.computeCost((StatisticsDTO) results.getResults().asList().get(0).getResultObject()),
                objValInMinutes, 0.2);

        final StatisticsDTO stats = Solvers.computeStats(solverInput, solverOutput);
        assertTrue(stats.toString(), obj.isValidResult(stats));
        assertEquals(objValInMinutes, obj.computeCost(stats), 0.1);
        assertEquals(objValInMinutes, decomposedCost(solverInput, solverOutput, obj), 0.01);
    }
}

From source file:com.milaboratory.core.motif.BitapPatternTest.java

@Test
public void testRandomMMIndelLast1() throws Exception {
    RandomGenerator rg = new Well19937c();
    long seed = rg.nextLong();
    rg = new Well19937c(seed);
    int its = TestUtil.its(1000, 100000);

    out: for (int i = 0; i < its; ++i) {
        NucleotideSequence seq = TestUtil.randomSequence(NucleotideSequence.ALPHABET, 5, 60);

        NucleotideSequence seqM = seq;// w  w  w  .ja  v  a 2  s .  co  m
        int muts = 1 + rg.nextInt(Math.min(10, seq.size()));
        for (int j = 0; j < muts; ++j)
            seqM = UniformMutationsGenerator.createUniformMutationAsObject(seqM, rg).mutate(seqM);

        NucleotideSequence seqLeft = TestUtil.randomSequence(NucleotideSequence.ALPHABET, 0, 40);
        NucleotideSequence seqRight = TestUtil.randomSequence(NucleotideSequence.ALPHABET, 0, 40);
        NucleotideSequence fullSeq = SequencesUtils.concatenate(seqLeft, seqM, seqRight);

        Motif<NucleotideSequence> motif = new Motif<>(seq);
        BitapPattern bitapPattern = motif.toBitapPattern();
        BitapMatcher bitapMatcher = bitapPattern.substitutionAndIndelMatcherLast(muts, fullSeq);

        boolean found = false;

        int pos;
        while ((pos = bitapMatcher.findNext()) >= 0) {
            if (pos == seqLeft.size() + seqM.size() - 1)
                found = true;
        }

        assertTrue("On iteration = " + i + " with seed " + seed, found);
    }
}

From source file:com.milaboratory.core.motif.BitapPatternTest.java

@Test
public void testRandomMMIndelFirst1() throws Exception {
    RandomGenerator rg = new Well19937c();
    long seed = rg.nextLong();
    rg = new Well19937c(seed);
    int its = TestUtil.its(1000, 100000);

    out: for (int i = 0; i < its; ++i) {
        NucleotideSequence seq = TestUtil.randomSequence(NucleotideSequence.ALPHABET, 5, 60);

        NucleotideSequence seqM = seq;//  w w w .  j a  va  2s .com
        int muts = 1 + rg.nextInt(Math.min(10, seq.size()));
        for (int j = 0; j < muts; ++j)
            seqM = UniformMutationsGenerator.createUniformMutationAsObject(seqM, rg).mutate(seqM);

        NucleotideSequence seqLeft = TestUtil.randomSequence(NucleotideSequence.ALPHABET, 0, 40);
        NucleotideSequence seqRight = TestUtil.randomSequence(NucleotideSequence.ALPHABET, 0, 40);
        NucleotideSequence fullSeq = SequencesUtils.concatenate(seqLeft, seqM, seqRight);

        Motif<NucleotideSequence> motif = new Motif<>(seq);
        BitapPattern bitapPattern = motif.toBitapPattern();
        BitapMatcher bitapMatcher = bitapPattern.mismatchAndIndelMatcherFirst(muts, fullSeq);

        boolean found = false;

        int pos;
        while ((pos = bitapMatcher.findNext()) >= 0) {
            if (pos == seqLeft.size())
                found = true;
        }

        assertTrue("On iteration = " + i + " with seed " + seed, found);
    }
}

From source file:com.milaboratory.core.motif.BitapPatternTest.java

@Test
public void testRandomMM1() throws Exception {
    RandomGenerator rg = new Well19937c();
    long seed = rg.nextLong();
    rg = new Well19937c(seed);
    int its = TestUtil.its(1000, 100000);

    out: for (int i = 0; i < its; ++i) {
        NucleotideSequence seq = TestUtil.randomSequence(NucleotideSequence.ALPHABET, 5, 60);

        NucleotideSequence seqM = seq;//w  w  w  .ja  v a2  s .  co m
        int mms = 1 + rg.nextInt(Math.min(10, seq.size()));
        for (int j = 0; j < mms; ++j)
            seqM = UniformMutationsGenerator.createUniformMutationAsObject(seqM, rg, MutationType.Substitution)
                    .mutate(seqM);

        int realMMs = SequencesUtils.mismatchCount(seq, 0, seqM, 0, seqM.size());

        NucleotideSequence seqLeft = TestUtil.randomSequence(NucleotideSequence.ALPHABET, 0, 40);
        NucleotideSequence seqRight = TestUtil.randomSequence(NucleotideSequence.ALPHABET, 0, 40);
        NucleotideSequence fullSeq = SequencesUtils.concatenate(seqLeft, seqM, seqRight);

        Motif<NucleotideSequence> motif = new Motif<>(seq);
        BitapPattern bitapPattern = motif.toBitapPattern();

        // Not filtered

        BitapMatcher bitapMatcher = bitapPattern.substitutionOnlyMatcherFirst(mms, fullSeq);

        boolean found = false;

        int pos;
        while ((pos = bitapMatcher.findNext()) >= 0) {
            if (pos == seqLeft.size()) {
                found = true;
                assertEquals(realMMs, bitapMatcher.getNumberOfErrors());
            }
            assertTrue("On iteration = " + i + " with seed " + seed,
                    SequencesUtils.mismatchCount(fullSeq, pos, seq, 0, seq.size()) <= mms);
        }

        assertTrue("On iteration = " + i + " with seed " + seed, found);
    }
}

From source file:com.milaboratory.core.motif.BitapPatternTest.java

@Test
public void testRandomMM2() throws Exception {
    RandomGenerator rg = new Well19937c();
    long seed = rg.nextLong();
    rg = new Well19937c(seed);
    int its = TestUtil.its(1000, 100000);

    int e = 0;//from w w w .j a  va 2 s .c o  m

    out: for (int i = 0; i < its; ++i) {
        NucleotideSequence seq = TestUtil.randomSequence(NucleotideSequence.ALPHABET, 10, 60);

        NucleotideSequence seqM = seq;
        int mms = 1 + rg.nextInt(3);
        for (int j = 0; j < mms; ++j)
            seqM = UniformMutationsGenerator.createUniformMutationAsObject(seqM, rg, MutationType.Substitution)
                    .mutate(seqM);

        int realMMs = SequencesUtils.mismatchCount(seq, 0, seqM, 0, seqM.size());

        NucleotideSequence seqLeft = TestUtil.randomSequence(NucleotideSequence.ALPHABET, 0, 40);
        NucleotideSequence seqRight = TestUtil.randomSequence(NucleotideSequence.ALPHABET, 0, 40);
        NucleotideSequence fullSeq = SequencesUtils.concatenate(seqLeft, seqM, seqRight);

        Motif<NucleotideSequence> motif = new Motif<>(seq);
        BitapPattern bitapPattern = motif.toBitapPattern();

        // Filtered

        BitapMatcherFilter bitapMatcher = new BitapMatcherFilter(
                bitapPattern.substitutionOnlyMatcherFirst(mms, fullSeq));

        boolean found = false;

        int pos;
        while ((pos = bitapMatcher.findNext()) >= 0) {
            if (pos == seqLeft.size()) {
                found = true;
                assertEquals(realMMs, bitapMatcher.getNumberOfErrors());
            }
            assertTrue("On iteration = " + i + " with seed " + seed,
                    SequencesUtils.mismatchCount(fullSeq, pos, seq, 0, seq.size()) <= mms);
        }

        if (!found)
            ++e;
    }

    assertTrue(e <= Math.max(5E-5 * its, 1.0));
}

From source file:com.cloudera.oryx.kmeans.computation.cluster.KMeansParallel.java

/**
 * Main constructor that includes the option to uses a fixed {@code Random} instance
 * for running the k-means algorithm for testing purposes.
 */// w w w  . j av a 2 s.  c o m
public KMeansParallel(RandomGenerator random, int projectionBits, int projectionSamples) {
    this.projectionBits = projectionBits;
    this.projectionSamples = projectionSamples;
    if (random == null) {
        this.seed = System.currentTimeMillis();
    } else {
        this.seed = random.nextLong();
    }
}

From source file:com.github.rinde.rinsim.scenario.generator.TimeWindowsTest.java

/**
 * Test whether calling generate with the same seed yields equal results.
 *//*w w w  .  jav a2  s  .  c o  m*/
@Test
public void determinismTest() {
    final RandomGenerator rng = new MersenneTwister(123L);
    for (final TravelTimes tt : DistanceTT.values()) {
        for (final Parcel.Builder parcelBuilder : parcelBuilders()) {
            for (int i = 0; i < 10; i++) {
                final long seed = rng.nextLong();
                timeWindowGenerator.generate(seed, parcelBuilder, tt, END_TIME);
                final TimeWindow p1 = parcelBuilder.getPickupTimeWindow();
                final TimeWindow d1 = parcelBuilder.getDeliveryTimeWindow();

                timeWindowGenerator.generate(seed, parcelBuilder, tt, END_TIME);
                final TimeWindow p2 = parcelBuilder.getPickupTimeWindow();
                final TimeWindow d2 = parcelBuilder.getDeliveryTimeWindow();
                assertNotSame(p1, p2);
                assertNotSame(d1, d2);
                assertEquals(p1, p2);
                assertEquals(d1, d2);
            }
        }
    }
}