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

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

Introduction

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

Prototype

public SynchronizedRandomGenerator(RandomGenerator rng) 

Source Link

Document

Creates a synchronized wrapper for the given RandomGenerator instance.

Usage

From source file:com.ibm.bi.dml.runtime.util.PoissonPRNGenerator.java

public void setup(double mean, long sd) {
    seed = sd;//w w w  .j  av  a 2  s .  com

    SynchronizedRandomGenerator srg = new SynchronizedRandomGenerator(new Well1024a());
    srg.setSeed(seed);
    _pdist = new PoissonDistribution(srg, _mean, PoissonDistribution.DEFAULT_EPSILON,
            PoissonDistribution.DEFAULT_MAX_ITERATIONS);
}

From source file:edu.cmu.tetrad.sem.LargeSemSimulator.java

public BoxDataSet simulateDataAcyclicConcurrent(int sampleSize) {
    int numVars = variableNodes.size();
    setupModel(numVars);/*  ww w .  j  ava2 s. co m*/

    System.out.println("Tier ordering");

    final int[][] _parents = parents;
    final double[][] _coefs = coefs;

    //        final double[][] _data = new double[sampleSize][numVars];
    final double[][] _data = new double[numVars][sampleSize];

    System.out.println("Starting simulation task");

    // This random number generator is not thread safe, so we make a new one each time.
    RandomGenerator apacheGen = new Well19937a(new Date().getTime());
    final RandomDataGenerator generator = new RandomDataGenerator(new SynchronizedRandomGenerator(apacheGen));

    //Do the simulation.
    class SimulationTask extends RecursiveTask<Boolean> {
        private int chunk;
        private int from;
        private int to;

        public SimulationTask(int chunk, int from, int to) {
            this.chunk = chunk;
            this.from = from;
            this.to = to;
        }

        @Override
        protected Boolean compute() {
            RandomGenerator apacheGen = new Well44497b(generator.nextLong(0, Long.MAX_VALUE));
            RandomDataGenerator generatorLocal = new RandomDataGenerator(apacheGen);

            if (to - from <= chunk) {
                for (int row = from; row < to; row++) {
                    if (row % 100 == 0)
                        out.println("Row " + row);

                    for (int i = 0; i < tierIndices.length; i++) {
                        int col = tierIndices[i];

                        double value = generatorLocal.nextGaussian(0, sqrt(errorVars[col]));

                        for (int j = 0; j < _parents[col].length; j++) {
                            int parent = _parents[col][j];
                            final double coef = _coefs[col][j];
                            final double v = _data[parent][row];
                            value += v * coef;

                            if (Double.isNaN(value)) {
                                throw new IllegalArgumentException();
                            }
                        }

                        value += means[col];

                        _data[col][row] = value;
                    }
                }

                return true;
            } else {
                List<SimulationTask> simulationTasks = new ArrayList<SimulationTask>();

                int mid = (to - from) / 2;

                simulationTasks.add(new SimulationTask(chunk, from, from + mid));
                simulationTasks.add(new SimulationTask(chunk, from + mid, to));

                invokeAll(simulationTasks);

                return true;
            }
        }
    }

    int chunk = 25;

    pool.invoke(new SimulationTask(chunk, 0, sampleSize));

    return new BoxDataSet(new VerticalDoubleDataBox(_data), variableNodes);
    //        return ColtDataSet.makeContinuousData(variableNodes, _data);
}

From source file:org.asoem.greyfish.utils.math.RandomGenerators.java

/**
 * Create a thread safe {@code RandomGenerator} wrapping given {@code randomGenerator}.
 *
 * @param randomGenerator the generator to wrap
 * @return a thread safe {@code RandomGenerator}
 * @see SynchronizedRandomGenerator//w w w. j a va2s.  c  o m
 */
public static RandomGenerator synchronizedGenerator(final RandomGenerator randomGenerator) {
    if (randomGenerator instanceof SynchronizedRandomGenerator) {
        return randomGenerator;
    } else {
        return new SynchronizedRandomGenerator(randomGenerator);
    }
}

From source file:org.nd4j.linalg.api.rng.DefaultRandom.java

public DefaultRandom(long seed) {
    this.seed = seed;
    this.randomGenerator = new SynchronizedRandomGenerator(new MersenneTwister(seed));
}