Example usage for org.apache.commons.math3.random MersenneTwister MersenneTwister

List of usage examples for org.apache.commons.math3.random MersenneTwister MersenneTwister

Introduction

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

Prototype

public MersenneTwister() 

Source Link

Document

Creates a new random number generator.

Usage

From source file:com.sop4j.SimpleStatistics.java

public static void main(String[] args) {
    final MersenneTwister rng = new MersenneTwister(); // used for RNG... READ THE DOCS!!!
    final int[] values = new int[NUM_VALUES];

    final DescriptiveStatistics descriptiveStats = new DescriptiveStatistics(); // stores values
    final SummaryStatistics summaryStats = new SummaryStatistics(); // doesn't store values
    final Frequency frequency = new Frequency();

    // add numbers into our stats
    for (int i = 0; i < NUM_VALUES; ++i) {
        values[i] = rng.nextInt(MAX_VALUE);

        descriptiveStats.addValue(values[i]);
        summaryStats.addValue(values[i]);
        frequency.addValue(values[i]);//from   w  w  w  . ja v a2 s  .  c o  m
    }

    // print out some standard stats
    System.out.println("MIN: " + summaryStats.getMin());
    System.out.println("AVG: " + String.format("%.3f", summaryStats.getMean()));
    System.out.println("MAX: " + summaryStats.getMax());

    // get some more complex stats only offered by DescriptiveStatistics
    System.out.println("90%: " + descriptiveStats.getPercentile(90));
    System.out.println("MEDIAN: " + descriptiveStats.getPercentile(50));
    System.out.println("SKEWNESS: " + String.format("%.4f", descriptiveStats.getSkewness()));
    System.out.println("KURTOSIS: " + String.format("%.4f", descriptiveStats.getKurtosis()));

    // quick and dirty stats (need a little help from Guava to convert from int[] to double[])
    System.out.println("MIN: " + StatUtils.min(Doubles.toArray(Ints.asList(values))));
    System.out.println("AVG: " + String.format("%.4f", StatUtils.mean(Doubles.toArray(Ints.asList(values)))));
    System.out.println("MAX: " + StatUtils.max(Doubles.toArray(Ints.asList(values))));

    // some stats based upon frequencies
    System.out.println("NUM OF 7s: " + frequency.getCount(7));
    System.out.println("CUMULATIVE FREQUENCY OF 7: " + frequency.getCumFreq(7));
    System.out.println("PERCENTAGE OF 7s: " + frequency.getPct(7));
}

From source file:edu.byu.nlp.data.app.DataExporter.java

public static void main(String[] args) throws IOException {
    args = new ArgumentParser(DataExporter.class).parseArgs(args).getPositionalArgs();

    RandomGenerator rnd = new MersenneTwister();
    Dataset dataset = readData(rnd);/*w  w w.j av a2  s.com*/

    Iterable<String> it = Iterables.transform(dataset, new Instance2SVMLitePlus());
    if (args.length < 1) {
        Writers.writeLines(new PrintWriter(new BufferedOutputStream(System.out)), it);
    } else {
        Files2.writeLines(it, args[0]);
    }
}

From source file:fi.smaa.common.RandomUtil.java

public static RandomUtil createWithRandomSeed() {
    return new RandomUtil(new MersenneTwister());
}

From source file:net.openhft.chronicle.timeseries.Columns.java

public static void generateBrownian(DoubleColumn col, double start, double end, double sd) {
    long length = col.length();
    double sd2 = sd / Math.sqrt(length);
    NormalDistribution nd = new NormalDistribution(0, sd2 * CHUNK_SIZE);
    int trendLength = Math.toIntExact((length - 1) / CHUNK_SIZE + 2);
    BytesStore trend = NativeBytesStore.lazyNativeBytesStoreWithFixedCapacity(trendLength * 8L);
    double x = start;
    RandomGenerator rand = new MersenneTwister();
    for (int i = 0; i < trendLength - 1; i++) {
        float f = rand.nextFloat();
        trend.writeDouble((long) i << 3, x);
        x += nd.inverseCumulativeProbability(f);
    }/*from   w w  w .  j a  v  a 2 s  .com*/
    trend.writeDouble((long) (trendLength - 1) << 3, x);
    double diff = end - x;
    double gradient = diff / (trendLength - 1);
    for (int i = 0; i < trendLength; i++) {
        double y = trend.addAndGetDoubleNotAtomic((long) i << 3, i * gradient);
        //            System.out.println(i + ": "+y);
    }
    int procs = Runtime.getRuntime().availableProcessors();
    int chunksPerTask = (trendLength - 1) / procs + 1;
    ForkJoinPool fjp = ForkJoinPool.commonPool();
    List<ForkJoinTask> tasks = new ArrayList<>(procs);
    for (int i = 0; i < procs; i++) {
        int si = i * chunksPerTask;
        int ei = Math.min(trendLength, si + chunksPerTask);
        tasks.add(fjp.submit(() -> {
            NormalDistribution nd2 = new NormalDistribution(0, sd2);
            RandomGenerator rand2 = new MersenneTwister();
            for (int j = si; j < ei; j++) {
                generateBrownian(col, (long) j * CHUNK_SIZE, trend.readDouble((long) j << 3),
                        trend.readDouble((long) (j + 1) << 3), nd2, rand2);
            }
        }));
    }
    for (ForkJoinTask task : tasks) {
        task.join();
    }
    trend.release();
}

From source file:fingerprints.helper.RandomNumber.java

/**
 * Mersenne Twister Random Number//from w w w  .  j av  a  2  s  .  co m
 *
 * @param maximum
 * @return
 */
public static long generateMersenneTwisterRandomNumber(int maximum) {
    RandomGenerator rg = new RandomAdaptor(new MersenneTwister());
    return rg.nextInt(maximum);
}

From source file:hivemall.math.random.CommonsMathRandom.java

public CommonsMathRandom() {
    this.rng = new MersenneTwister();
}

From source file:net.modelbased.proasense.storage.writer.RandomEventGenerator.java

public RandomEventGenerator() {
    this.randomData = new RandomDataGenerator();
    this.randomNumber = new MersenneTwister();
}

From source file:edu.oregonstate.eecs.mcplan.domains.cards.InfiniteDeck.java

public InfiniteDeck() {
    rng_ = new MersenneTwister();
}

From source file:edu.byu.nlp.util.IntArraysTest.java

@Test
public void testShuffled() {
    int[] s = IntArrays.shuffled(IntArrays.sequence(0, 3), new MersenneTwister());
    Set<Integer> answers = Sets.newHashSet(0, 1, 2);
    assertThat(s.length).isEqualTo(3);//from  w  w w.j  a va 2s  . c om
    assertThat(answers.contains(s[0])).isTrue();
    assertThat(answers.contains(s[1])).isTrue();
    assertThat(answers.contains(s[2])).isTrue();
    assertThat(s[0] != s[1] && s[1] != s[2] && s[2] != s[0]).isTrue();
}

From source file:com.vsthost.rnd.SandboxStrategyTest.java

@Test
public void isItEvolving() {
    // Create an instance of a problem:
    Problem problem = new Problem(new double[] { -1, -1 }, new double[] { 1, 1 });

    // Define an objective:
    Objective objective = candidate -> Math.abs(SillyFormula(candidate[0], candidate[1]));

    // Define a strategy:
    Strategy strategy = new SandboxStrategy(0.75, 0.8, 0.1, new MersenneTwister());

    // Initialize a population:
    Population population = new Population(20, 2, new double[] { -1, -1 }, new double[] { 1, 1 },
            new UniformRealDistribution());

    // Define the diagnostics:
    Diagnostics diagnostics = new Diagnostics(true, true);

    // Define the DE instance:
    DEoptim DEoptim = new DEoptim(50, problem, objective, strategy, population, diagnostics);

    // Run it:// w w w.j  a v a 2 s .  com
    DEoptim.evolve();

    // Compare the first score to the best:
    assertTrue(diagnostics.getEntries().get(0).score >= diagnostics.getBestScore());
}