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:jprobix.ui.SPlotFinal.java

private static XYDataset samplexydataset2() {
    int cols = 20;
    int rows = 20;
    double[][] values = new double[cols][rows];

    XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
    XYSeries series = new XYSeries("Random");

    Random rand = new Random();
    for (int i = 0; i < values.length; i++) {
        for (int j = 0; j < values.length; j++) {
            double x = Math.round(rand.nextDouble() * 500);
            double y = Math.round(rand.nextDouble() * 500);

            series.add(x, y);//from w ww.  j  av a2  s .  co  m
        }
    }
    xySeriesCollection.addSeries(series);
    return xySeriesCollection;

}

From source file:audio.cords.old.RegressionDemo.java

private static XYSeriesCollection getTestData() {
    Random rg = new Random();
    XYSeriesCollection data = new XYSeriesCollection();
    for (int i = 1; i <= 3; i++) {
        XYSeries series = new XYSeries("Series " + i);
        double a = rg.nextDouble() - .5;
        int b = rg.nextInt(20) - 10;
        for (int j = 1; j <= 20; j++) {
            double x = j + (rg.nextDouble() - .5);
            double y = a * j + b + (rg.nextDouble() - .5) * 2;
            series.add(x, y);//from w w  w.j  a va  2 s  . co m
        }
        data.addSeries(series);
    }
    return data;
}

From source file:com.clust4j.TestSuite.java

public static Array2DRowRealMatrix getRandom(final int rows, final int cols) {
    final Random rand = new Random();
    final double[][] data = new double[rows][cols];

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            data[i][j] = rand.nextDouble() * (rand.nextDouble() > 0.5 ? -1 : 1);

    return new Array2DRowRealMatrix(data, false);
}

From source file:org.meteoinfo.math.RandomUtil.java

/**
 * Get random array - one dimension/*from   w ww  .j a  v  a 2  s .  c o  m*/
 *
 * @param n Array length
 * @return Result array
 */
public static Array rand(int n) {
    Array r = Array.factory(DataType.DOUBLE, new int[] { n });
    Random rd = new Random();
    for (int i = 0; i < r.getSize(); i++) {
        r.setDouble(i, rd.nextDouble());
    }

    return r;
}

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

public static SimpleFeature createFeature(SimpleFeatureBuilder builder, int i, String visibility) {
    final String[] PEOPLE_NAMES = { "James", "John", "Peter", "Hannah", "Claire", "Gabriel" };
    final Random random = new Random();

    Double lat = random.nextDouble() * 180 - 90;

    builder.reset();//from   w  w  w .  ja  v a  2s .  co m
    builder.add(PEOPLE_NAMES[i % PEOPLE_NAMES.length]); // name

    builder.add((int) Math.round(random.nextDouble() * 110)); // age
    builder.add(random.nextDouble());
    builder.add(lat);
    builder.add(new Date()); // dtg
    builder.add(WKTUtils$.MODULE$.read("POINT(" + -180.0 + " " + lat + ")")); // geom
    SimpleFeature feat = builder.buildFeature(Integer.toString(i));
    if (visibility != null) {
        feat.getUserData().put("geomesa.feature.visibility", visibility);
    }
    return feat;
}

From source file:org.deeplearning4j.examples.recurrent.character.LSTMCharModellingExample.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
 *//*www  .  j a  v a  2 s.  c  o  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:org.matsim.contrib.parking.parkingsearch.ParkingUtils.java

public static Coord getRandomPointAlongLink(Random rnd, Link link) {
    Coord fromNodeCoord = link.getFromNode().getCoord();
    Coord toNodeCoord = link.getToNode().getCoord();
    double r = rnd.nextDouble();

    double x = (fromNodeCoord.getX() * r) + (toNodeCoord.getX() * (1 - r));
    double y = (fromNodeCoord.getY() * r) + (toNodeCoord.getY() * (1 - r));

    return new Coord(x, y);
}

From source file:org.meteoinfo.math.RandomUtil.java

/**
 * Get random array/*from w w w  .j a  v a2 s  .  com*/
 *
 * @param shape Shape
 * @return Array Result array
 */
public static Array rand(List<Integer> shape) {
    int[] ashape = new int[shape.size()];
    for (int i = 0; i < shape.size(); i++) {
        ashape[i] = shape.get(i);
    }
    Array a = Array.factory(DataType.DOUBLE, ashape);
    Random rd = new Random();
    for (int i = 0; i < a.getSize(); i++) {
        a.setDouble(i, rd.nextDouble());
    }

    return a;
}

From source file:edu.berkeley.sparrow.examples.BackendBenchmarkProfiler.java

/**
 * This generates an arrival delay according to an exponential distribution with
 * average 1\{@code lambda}. Generating arrival delays from such a distribution creates
 * a Poission process with an average arrival rate of {@code lambda} events per second. 
 *//*from   w  w  w.j  av  a 2 s .co m*/
public static double generateInterarrivalDelay(Random r, double lambda) {
    double u = r.nextDouble();
    return -Math.log(u) / lambda;
}

From source file:org.apache.metron.statistics.sampling.UniformSamplerTest.java

@BeforeClass
public static void beforeClass() {
    Random rng = new Random(0);
    GaussianRandomGenerator gen = new GaussianRandomGenerator(new MersenneTwister(0));
    for (int i = 0; i < SAMPLE_SIZE; ++i) {
        double us = 10 * rng.nextDouble();
        uniformSample.add(us);//from w w  w. j  av  a 2  s .c o  m
        uniformStats.addValue(us);
        double gs = 10 * gen.nextNormalizedDouble();
        gaussianSample.add(gs);
        gaussianStats.addValue(gs);
    }
}