Example usage for java.util Random nextDouble

List of usage examples for java.util Random nextDouble

Introduction

In this page you can find the example usage for java.util Random nextDouble.

Prototype

public double nextDouble() 

Source Link

Document

Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

Usage

From source file:org.ensor.fftmusings.rnn2.GravesLSTMCharModellingExample.java

/**
 * Given a probability distribution over discrete classes, sample from the
 * distribution and return the generated class index.
 *
 * @param distribution Probability distribution over classes. Must sum to
 * 1.0//from w w  w  .j  a  v a 2  s .co m
 */
public static int sampleFromDistribution(double[] distribution, Random rng) {
    double d = 0.0;
    double sum = 0.0;
    for (int t = 0; t < 10; t++) {
        d = rng.nextDouble();
        sum = 0.0;
        for (int i = 0; i < distribution.length; i++) {
            sum += distribution[i];
            if (d <= sum) {
                return i;
            }
        }
        //If we haven't found the right index yet, maybe the sum is slightly
        //lower than 1 due to rounding error, so try again.
    }
    //Should be extremely unlikely to happen if distribution is a valid probability distribution
    throw new IllegalArgumentException("Distribution is invalid? d=" + d + ", sum=" + sum);
}

From source file:com.insightml.evaluation.simulation.SplitSimulation.java

public static <S extends Sample> Pair<Iterable<S>, List<S>> split(final Iterable<S> instances,
        final double trainFraction, final Random random) {
    if (trainFraction == 1.0) {
        return new Pair<>(instances, null);
    }//from  w  ww.j  a v a 2s  .  co m
    final List<S> train = new LinkedList<>();
    final List<S> test = new LinkedList<>();
    for (final S sample : instances) {
        if (random.nextDouble() < trainFraction) {
            train.add(sample);
        } else {
            test.add(sample);
        }
    }
    return new Pair<>(train, test);
}

From source file:eu.stratosphere.nephele.services.iomanager.IOManagerITCase.java

private static final int skewedSample(Random rnd, int max) {
    double uniform = rnd.nextDouble();
    double var = Math.pow(uniform, 8.0);
    double pareto = 0.2 / var;

    int val = (int) pareto;
    return val > max ? val % max : val;
}

From source file:org.janusgraph.TestBed.java

private static final void doSomethingExpensive(int milliseconds) {
    double d = 0.0;
    Random r = new Random();
    for (int i = 0; i < 10000 * milliseconds; i++)
        d += Math.pow(1.1, r.nextDouble());

}

From source file:com.phoenixst.plexus.examples.RandomGraphFactory.java

/**
 *  Creates a random graph according to the Watts-Strogatz model.
 *  The number <code>d</code> here is half of <code>K</code> in
 *  the standard literature.// ww  w  .  ja v a2 s.c  om
 *
 *  <P>Start with a circulant graph.  Arrange the nodes in a
 *  circle, starting at 0 and increasing, in order, clockwise.
 *  Begin with node 0 and the edge which connects it to its
 *  nearest clockwise neighbor, which is node 1.  With probability
 *  <code>prob</code>, reconnect this edge from node 0 to a
 *  uniformly randomly selected node, with duplicate and self edges
 *  forbidden.  Repeat this process for each node, moving
 *  clockwise around the circle.  Now, repeat the entire cycle,
 *  but instead choose edges which connect nodes to their
 *  second-nearest clockwise neighbor.  And so on, until every one
 *  of the original edges has been considered.
 */
public static Graph createWattsStrogatz(int n, int d, double prob) {
    if (prob < 0.0 || prob > 1.0) {
        throw new IllegalArgumentException("Probability must be between 0.0 and 1.0, inclusive.");
    }
    Graph graph = new DefaultGraph(new CirculantGraph(n, d));
    Random random = new Random();
    for (int dist = 1; dist <= d; dist++) {
        for (int nodeIndex = 0; nodeIndex < n; nodeIndex++) {
            if (random.nextDouble() < prob) {
                Object tail = new Integer(nodeIndex);
                Object head = new Integer((nodeIndex + dist) % n);
                Predicate edgePred = EdgePredicateFactory.createEqualsNodes(tail, head,
                        GraphUtils.ANY_DIRECTION_MASK);
                graph.removeEdge(graph.getEdge(edgePred));
                while (true) {
                    head = new Integer(random.nextInt(n));
                    Predicate traverserPred = TraverserPredicateFactory.createEqualsNode(head,
                            GraphUtils.ANY_DIRECTION_MASK);
                    if (!tail.equals(head) && graph.getIncidentEdge(tail, traverserPred) == null) {
                        graph.addEdge(null, tail, head, true);
                        break;
                    }
                }
            }
        }
    }
    return graph;
}

From source file:edu.iu.daal_als.SGDUtil.java

public static void randomize(Random random, double[] row, int size, double oneOverSqrtR) {
    // Non-zero initialization
    for (int i = 0; i < size; i++) {
        double rowi = 0.0;
        do {//from w w w  . j  av  a 2 s.  co m
            rowi = random.nextDouble();
        } while (rowi == 0.0);
        row[i] = rowi * oneOverSqrtR;
    }
}

From source file:com.example.geomesa.kafka.KafkaQuickStart.java

public static void addDeleteNewFeature(SimpleFeatureType sft, FeatureStore producerFS)
        throws InterruptedException, IOException {
    SimpleFeatureBuilder builder = new SimpleFeatureBuilder(sft);
    DefaultFeatureCollection featureCollection = new DefaultFeatureCollection();
    final Random random = new Random();

    String id = "1000";

    builder.add("Antoninus"); // name
    builder.add((int) Math.round(random.nextDouble() * 110)); // age
    builder.add(new Date()); // dtg
    builder.add(WKTUtils$.MODULE$.read("POINT(-1 -1)")); // geom
    SimpleFeature feature = builder.buildFeature(id);

    featureCollection.add(feature);/*from   w w w .jav a 2 s  .c  o  m*/
    producerFS.addFeatures(featureCollection);

    FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
    Filter idFilter = ff.id(ff.featureId(id));
    producerFS.removeFeatures(idFilter);
}

From source file:org.ensor.fftmusings.autoencoder.RNNTrainer.java

public static void evaluateModel(MultiLayerNetwork model, LossMixtureDensity cost,
        MultiLayerNetwork stackedAutoencoder, Random rng, int epoch) {

    //Create input for initialization
    INDArray initialInput = Nd4j.zeros(100);
    for (int i = 0; i < 100; i++) {
        initialInput.putScalar(i, rng.nextDouble());
    }/*from   ww  w  . ja  v  a2s.  c o  m*/

    model.rnnClearPreviousState();

    Iterator<INDArray> rnnSampleIterator = new RNNSampleIterator(model, cost, stackedAutoencoder, initialInput,
            rng);

    new Pipeline(new Layer.ToFFTPNG("data/daa/dct-magnitude-" + epoch + ".png", true))
            .add(new Layer.INDArrayToFFTD()).add(new FFTOverlap.NormalizeToHearing(false, 11025))
            .add(new FFTOverlap.ReversePhaseDelta(1024)).add(new ChannelDuplicator(AudioSample.class, 2))
            .add(WAVFileWriter.create("data/daa/sample-" + epoch + ".wav")).execute(rnnSampleIterator);

}

From source file:org.deeplearning4j.legacyExamples.rnn.SparkLSTMCharacterExample.java

/**
 * Given a probability distribution over discrete classes, sample from the distribution
 * and return the generated class index.
 *
 * @param distribution Probability distribution over classes. Must sum to 1.0
 *//*from  ww  w . j a  v  a 2s.com*/
private static int sampleFromDistribution(double[] distribution, Random rng) {
    double d = rng.nextDouble();
    double sum = 0.0;
    for (int i = 0; i < distribution.length; i++) {
        sum += distribution[i];
        if (d <= sum)
            return i;
    }
    //Should never happen if distribution is a valid probability distribution
    throw new IllegalArgumentException("Distribution is invalid? d=" + d + ", sum=" + sum);
}

From source file:view.ChooseFile.java

private static double generateCyclicRandomNumber() {
    Random r = new Random();
    double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();
    return randomValue;
}