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:at.salzburgresearch.kmt.zkconfig.ZookeeperConfigurationTest.java

@Test
public void testDouble() throws Exception {
    Configuration config = new ZookeeperConfiguration(zkConnection, 5000, "/test");

    final String key = UUID.randomUUID().toString();
    final Random random = new Random();
    final double val1 = random.nextDouble();
    final Double val2 = random.nextDouble();

    assertThat(config.getProperty(key), nullValue());

    config.setProperty(key, val1);
    assertEquals(val1, config.getDouble(key), 10e-6);
    assertEquals(Double.valueOf(val1), config.getDouble(key, val2));

    config.setProperty(key, val2);
    assertEquals(val2, config.getDouble(key), 10e-6);
    assertEquals(val2, config.getDouble(key, Double.valueOf(val1)));
}

From source file:com.facebook.presto.operator.aggregation.TestBootstrappedAggregation.java

@Test
public void testErrorBound() throws Exception {
    int trials = 20;
    BinomialDistribution binomial = new BinomialDistribution(trials, 0.5);

    int successes = 0;
    Random rand = new Random(0);
    for (int i = 0; i < trials; i++) {
        int sum = 1_000;
        PageBuilder builder = new PageBuilder(ImmutableList.of(BIGINT, BIGINT));
        for (int j = 0; j < sum; j++) {
            if (rand.nextDouble() < 0.5) {
                builder.getBlockBuilder(0).appendLong(1);
                builder.getBlockBuilder(1).appendLong(2);
            }//w  w w  . j ava  2 s.  c  o  m
        }

        AggregationFunction function = new DeterministicBootstrappedAggregation(
                createTestingBlockEncodingManager(), LONG_SUM);

        successes += approximateAggregationWithinErrorBound(function, 1, 0.5, (double) sum, builder.build()) ? 1
                : 0;
    }

    // Since we used a confidence of 0.5, successes should have a binomial distribution B(n=20, p=0.5)
    assertTrue(binomial.inverseCumulativeProbability(0.01) < successes
            && successes < binomial.inverseCumulativeProbability(0.99));
}

From source file:ml.shifu.shifu.core.dvarsel.wrapper.CandidateGenerator.java

private Integer randomVariable(Random random, List<Integer> variableList) {
    Integer variable = variables.get((int) (random.nextDouble() * (variables.size() - 1)));
    if (variableList.contains(variable)) {
        variable = randomVariable(random, variableList);
    }/*from   w w w .jav a 2  s  .  com*/
    return variable;
}

From source file:net.radai.garbanzo.GarbanzoTest.java

@Test
public void testRoundTrip() throws Exception {
    long seed = System.currentTimeMillis();
    Random random = new Random(seed);
    BeanClass original = new BeanClass();
    original.f1 = "";
    original.f2 = RandomStringUtils.randomAscii(10);
    original.f3 = random.nextDouble();
    original.f4 = null;//from  www .  ja  va2 s . co  m
    original.f5 = UUID.randomUUID();
    original.f6 = new byte[1 + random.nextInt(10)];
    random.nextBytes(original.f6);
    original.f7 = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        original.f7.add((long) random.nextInt(10));
    }
    original.f8 = new HashMap<>();
    original.f8.put(Enum1.V1, (short) 7);
    original.f8.put(Enum1.V2, null);
    original.f9 = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        InnerBeanClass inner = new InnerBeanClass();
        inner.f1 = "bob " + i;
        original.f9.add(inner);
    }
    original.f9.add(null);

    String serialized = Garbanzo.marshal(original);

    BeanClass deserialized = Garbanzo.unmarshall(BeanClass.class, serialized);

    Assert.assertEquals(original, deserialized);
}

From source file:org.broadinstitute.gatk.tools.walkers.variantrecalibration.MultivariateGaussian.java

public void initializeRandomMu(final Random rand) {
    for (int jjj = 0; jjj < mu.length; jjj++) {
        mu[jjj] = -4.0 + 8.0 * rand.nextDouble();
    }//from ww w .  j  av  a 2  s .  c  om
}

From source file:ml.shifu.shifu.core.binning.EqualPopulationBinningTest.java

/**
 * @return// ww w.ja va 2s  .c  o m
 */
private EqualPopulationBinning createBinning() {
    Random rd = new Random(System.currentTimeMillis());

    EqualPopulationBinning binning = new EqualPopulationBinning(20);
    for (int i = 0; i < 18000; i++) {
        binning.addData(Double.toString(rd.nextDouble() % 1000));
    }

    return binning;
}

From source file:org.apache.pig.test.TestPi.java

@Before
public void setUp() throws Exception {

    log.info("Generating test data...");
    log.info("Default block size = " + defaultBlockSize);
    log.info("Total no. of iterations to run for test data = " + total);

    Random rand = new Random();

    pig = new PigServer(ExecType.LOCAL);
    Data data = resetData(pig);/* w w  w .  j  a v a2  s.  co  m*/
    List<Tuple> theInput = Lists.newArrayList();

    for (int i = 0; i < total; i++) {
        Double x = new Double(rand.nextDouble());
        Double y = new Double(rand.nextDouble());
        double x1 = x.doubleValue() - 0.5;
        double y1 = y.doubleValue() - 0.5;
        double sq_dist = (x1 * x1) + (y1 * y1);
        if (sq_dist <= 0.25) {
            inCircle++;

        }

        theInput.add(tuple(x, y));

        totalLength += String.valueOf(sq_dist).length();
    }
    data.set("foo", theInput);
}

From source file:com.linkedin.pinot.index.writer.FixedByteWidthRowColDataFileWriterTest.java

@Test
public void testSingleColDouble() throws Exception {

    File wfile = new File("test_single_col_writer.dat");
    wfile.delete();/*w  ww  .j av a 2 s . c  o  m*/
    final int rows = 100;
    final int cols = 1;
    final int[] columnSizes = new int[] { 8 };
    FixedByteSingleValueMultiColWriter writer = new FixedByteSingleValueMultiColWriter(wfile, rows, cols,
            columnSizes);
    final double[] data = new double[rows];
    Random r = new Random();
    for (int i = 0; i < rows; i++) {
        data[i] = r.nextDouble();
        writer.setDouble(i, 0, data[i]);
    }
    writer.close();

    File rfile = new File("test_single_col_writer.dat");
    PinotDataBuffer buffer = PinotDataBuffer.fromFile(rfile, ReadMode.mmap, FileChannel.MapMode.READ_WRITE,
            "testing");
    FixedByteSingleValueMultiColReader reader = new FixedByteSingleValueMultiColReader(buffer, rows, cols,
            columnSizes);

    for (int i = 0; i < rows; i++) {
        Assert.assertEquals(reader.getDouble(i, 0), data[i]);
    }
    reader.close();
    rfile.delete();
}

From source file:org.deeplearning4j.clustering.util.MathUtils.java

/**
 * This will generate a series of uniformally distributed
 * numbers between l times/*from  ww w.jav a 2 s.c o m*/
 * @param l the number of numbers to generate
 * @return l uniformally generated numbers
 */
public static double[] generateUniform(int l) {
    double[] ret = new double[l];
    Random rgen = new Random();
    for (int i = 0; i < l; i++) {
        ret[i] = rgen.nextDouble();
    }
    return ret;
}

From source file:com.example.geomesa.accumulo.FeatureLevelVisibility.java

static FeatureCollection createNewFeatures(SimpleFeatureType simpleFeatureType, int numNewFeatures) {
    DefaultFeatureCollection featureCollection = new DefaultFeatureCollection();

    String id;//from  w w  w  .j av  a 2 s . c  o m
    Object[] NO_VALUES = {};
    String[] PEOPLE_NAMES = { "Addams", "Bierce", "Clemens" };
    Long SECONDS_PER_YEAR = 365L * 24L * 60L * 60L;
    Random random = new Random(5771);
    DateTime MIN_DATE = new DateTime(2014, 1, 1, 0, 0, 0, DateTimeZone.forID("UTC"));
    Double MIN_X = -78.0;
    Double MIN_Y = -39.0;
    Double DX = 2.0;
    Double DY = 2.0;

    for (int i = 0; i < numNewFeatures; i++) {
        // create the new (unique) identifier and empty feature shell
        id = "Observation." + Integer.toString(i);
        SimpleFeature simpleFeature = SimpleFeatureBuilder.build(simpleFeatureType, NO_VALUES, id);

        // be sure to tell GeoTools explicitly that you want to use the ID you provided
        simpleFeature.getUserData().put(Hints.USE_PROVIDED_FID, java.lang.Boolean.TRUE);

        // populate the new feature's attributes

        // string value
        simpleFeature.setAttribute("Who", PEOPLE_NAMES[i % PEOPLE_NAMES.length]);

        // long value
        simpleFeature.setAttribute("What", i);

        // location:  construct a random point within a 2-degree-per-side square
        double x = MIN_X + random.nextDouble() * DX;
        double y = MIN_Y + random.nextDouble() * DY;
        Geometry geometry = WKTUtils.read("POINT(" + x + " " + y + ")");

        // date-time:  construct a random instant within a year
        simpleFeature.setAttribute("Where", geometry);
        DateTime dateTime = MIN_DATE.plusSeconds((int) Math.round(random.nextDouble() * SECONDS_PER_YEAR));
        simpleFeature.setAttribute("When", dateTime.toDate());

        // another string value
        // "Why"; left empty, showing that not all attributes need values

        // set visibility on each feature, and attribute for display
        if (i % 2 == 0) {
            simpleFeature.setAttribute("Visibility", "admin");
            SecurityUtils.setFeatureVisibility(simpleFeature, "admin");
        } else {
            simpleFeature.setAttribute("Visibility", "user|admin");
            SecurityUtils.setFeatureVisibility(simpleFeature, "user|admin");
        }
        // accumulate this new feature in the collection
        featureCollection.add(simpleFeature);
    }

    return featureCollection;
}