Example usage for org.apache.commons.math.random RandomDataImpl nextInt

List of usage examples for org.apache.commons.math.random RandomDataImpl nextInt

Introduction

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

Prototype

public int nextInt(int lower, int upper) 

Source Link

Document

Generate a random int value uniformly distributed between lower and upper, inclusive.

Usage

From source file:org.sapegin.bgp.analyse.spikes.SelectedSpikes.java

/**
 * get <numberOfRandomSpikesToAnalyse> random spikes from spike collection
 * /*w  w  w .j a  v a  2s  .  co m*/
 * @param numberOfRandomSpikesToAnalyse
 * @return
 */
public SpikeCollection getRandom(int numberOfRandomSpikesToAnalyse) {

    SpikeCollection randomSpikes = new SpikeCollection();

    RandomDataImpl generator = new RandomDataImpl();

    // find total number of spikes
    int total = 0;
    for (SingleASspikes asSpikes : allUpdates.values()) {
        total = total + asSpikes.getNumberOfSpikes();
    }

    // check if I have enough spikes
    if (numberOfRandomSpikesToAnalyse > total) {
        numberOfRandomSpikesToAnalyse = total;
    }

    // get needed number of random spikes
    int selected = 0;
    ArrayList<MonitoredAS> monitoredASs = new ArrayList<MonitoredAS>(allUpdates.keySet());

    while (selected < numberOfRandomSpikesToAnalyse) {
        // get random key
        MonitoredAS as;
        if (monitoredASs.size() > 1) {
            as = monitoredASs.get(generator.nextInt(0, monitoredASs.size() - 1));
        } else {
            as = monitoredASs.get(0);
        }

        // get random spike
        SingleASspikes oneASspikes = allUpdates.get(as);
        // ArrayList<Long> seconds = new
        // ArrayList<Long>(oneASspikes.keySet());

        long time;
        long minTime = oneASspikes.getCurrentMinTime();
        long maxTime = oneASspikes.getCurrentMaxTime();
        if (minTime < maxTime) {
            time = generator.nextLong(oneASspikes.getCurrentMinTime(), oneASspikes.getCurrentMaxTime());
        } else {
            time = minTime;
        }

        if (oneASspikes.hasSpikeAtTime(time)) {
            Spike randomSpike = new Spike(oneASspikes.getSpikeAtTime(time).copyPrefixSet());

            // add it to map with all random spikes
            if (randomSpikes.addSpike(time, randomSpike, as)) {
                selected++;
            }
        }
    }

    return randomSpikes;
}