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

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

Introduction

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

Prototype

public int nextInt(int n) throws IllegalArgumentException 

Source Link

Document

This default implementation is copied from Apache Harmony java.util.Random (r929253).

Implementation notes:

  • If n is a power of 2, this method returns (int) ((n * (long) next(31)) >> 31) .
  • If n is not a power of 2, what is returned is next(31) % n with next(31) values rejected (i.e.

    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  www  . jav  a2s.  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));
    }