Example usage for org.apache.commons.math3.random RandomDataGenerator nextBinomial

List of usage examples for org.apache.commons.math3.random RandomDataGenerator nextBinomial

Introduction

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

Prototype

public int nextBinomial(int numberOfTrials, double probabilityOfSuccess) 

Source Link

Document

Generates a random value from the BinomialDistribution Binomial Distribution .

Usage

From source file:com.siemens.industrialbenchmark.util.RandomNumberExpectedValueTest.java

@Test
public void testExpectedValues() {

    long seed = 0;
    Random rand = new Random(seed);
    RandomDataGenerator randomData = new RandomDataGenerator();

    double uniformAverage = 0.0;
    double binomialAverage = 0.0;
    double normalAverage = 0.0;
    double exponentialAverage = 0.0;

    for (int i = 0; i < 1e6; i++) {

        // set current seed
        randomData.reSeed(seed);/*from w w w  .java 2 s .c  om*/

        // draw random numbers
        double n = randomData.nextGaussian(0, 1);
        double u = randomData.nextUniform(0, 1);
        double b = randomData.nextBinomial(1, 0.5);
        double e = randomData.nextExponential(0.25);

        // average mean random number
        uniformAverage += (1. / (1. + i)) * (u - uniformAverage);
        binomialAverage += (1. / (1. + i)) * (b - binomialAverage);
        normalAverage += (1. / (1. + i)) * (n - normalAverage);
        exponentialAverage += (1. / (1. + i)) * (e - exponentialAverage);

        // draw new seed from global random generator
        seed = rand.nextLong();
    }

    assertEquals(0.5, uniformAverage, 0.001);
    assertEquals(0.5, binomialAverage, 0.001);
    assertEquals(0.0, normalAverage, 0.001);
    assertEquals(0.25, exponentialAverage, 0.001);
}

From source file:gdsc.smlm.ij.plugins.pcpalm.PCPALMMolecules.java

private int getBlinks(RandomDataGenerator dataGenerator, double averageBlinks) {
    switch (blinkingDistribution) {
    case 3:// ww w  . j  a  v  a 2  s .c  om
        // Binomial distribution
        return dataGenerator.nextBinomial((int) Math.round(averageBlinks), p);

    case 2:
        return (int) Math.round(averageBlinks);
    case 1:
        return StandardFluorophoreSequenceModel.getBlinks(true, dataGenerator, averageBlinks);
    default:
        return StandardFluorophoreSequenceModel.getBlinks(false, dataGenerator, averageBlinks);
    }
}