List of usage examples for java.util Random nextDouble
public double nextDouble()
From source file:eu.amidst.core.exponentialfamily.EF_InverseGamma.java
/** * {@inheritDoc}// w ww .ja v a2 s . c o m */ @Override public EF_UnivariateDistribution randomInitialization(Random random) { double alpha = random.nextGaussian() * 2 + 1; double beta = random.nextDouble() * 1 + 0.1; this.getNaturalParameters().set(0, -alpha - 1); this.getNaturalParameters().set(1, -beta); this.fixNumericalInstability(); this.updateMomentFromNaturalParameters(); return this; }
From source file:beast.math.distribution.GammaDistributionTest.java
@Test public void testPdf() { final int numberOfTests = 100; double totErr = 0; double ptotErr = 0; int np = 0;//from www . j a v a 2 s . com double qtotErr = 0; Random random = new Random(37); for (int i = 0; i < numberOfTests; i++) { final double mean = .01 + (3 - 0.01) * random.nextDouble(); final double var = .01 + (3 - 0.01) * random.nextDouble(); final double scale = var / mean; final double shape = mean / scale; final GammaDistribution gamma = new GammaDistribution(shape, scale); final double value = gamma.nextGamma(); final double mypdf = mypdf(value, shape, scale); final double pdf = gamma.pdf(value); if (Double.isInfinite(mypdf) && Double.isInfinite(pdf)) { continue; } assertFalse(Double.isNaN(mypdf)); assertFalse(Double.isNaN(pdf)); totErr += mypdf != 0 ? Math.abs((pdf - mypdf) / mypdf) : pdf; assertFalse("nan", Double.isNaN(totErr)); //assertEquals("" + shape + "," + scale + "," + value, mypdf,gamma.pdf(value),1e-10); final double cdf = gamma.cdf(value); UnivariateFunction f = new UnivariateFunction() { public double value(double v) { return mypdf(v, shape, scale); } }; final UnivariateIntegrator integrator = new RombergIntegrator(MachineAccuracy.SQRT_EPSILON, 1e-14, 1, 16); double x; try { x = integrator.integrate(16, f, 0.0, value); ptotErr += cdf != 0.0 ? Math.abs(x - cdf) / cdf : x; np += 1; //assertTrue("" + shape + "," + scale + "," + value + " " + Math.abs(x-cdf)/x + "> 1e-6", Math.abs(1-cdf/x) < 1e-6); //System.out.println(shape + "," + scale + " " + value); } catch (MaxCountExceededException e) { // can't integrate , skip test // System.out.println(shape + "," + scale + " skipped"); } final double q = gamma.quantile(cdf); qtotErr += q != 0 ? Math.abs(q - value) / q : value; // assertEquals("" + shape + "," + scale + "," + value + " " + Math.abs(q-value)/value, q, value, 1e-6); } //System.out.println( !Double.isNaN(totErr) ); // System.out.println(totErr); // bad test, but I can't find a good threshold that works for all individual cases assertTrue("failed " + totErr / numberOfTests, totErr / numberOfTests < 1e-7); assertTrue("failed " + ptotErr / np, np > 0 ? (ptotErr / np < 1e-5) : true); assertTrue("failed " + qtotErr / numberOfTests, qtotErr / numberOfTests < 1e-7); }
From source file:org.orekit.utils.FieldAngularCoordinatesTest.java
private FieldRotation<DerivativeStructure> randomRotation(Random random) { double q0 = random.nextDouble() * 2 - 1; double q1 = random.nextDouble() * 2 - 1; double q2 = random.nextDouble() * 2 - 1; double q3 = random.nextDouble() * 2 - 1; double q = FastMath.sqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3); return createRotation(q0 / q, q1 / q, q2 / q, q3 / q, false); }
From source file:edu.cornell.med.icb.goby.algorithmic.algorithm.TestComputeStartCount.java
/** * @param lo lower limit of range//from w w w .j a v a2s. co m * @param hi upper limit of range * @return a random integer in the range <STRONG>lo</STRONG>, * <STRONG>lo</STRONG>+1, ... ,<STRONG>hi</STRONG> */ private int chooseRandom(Random random, final int lo, final int hi) { final double r = random.nextDouble(); int result = (int) ((long) lo + (long) ((1L + (long) hi - (long) lo) * r)); assert result >= lo && result <= hi; return result; }
From source file:com.caseystella.analytics.distribution.DistributionTest.java
@Test public void testQuantiles() { Random r = new Random(0); List<DataPoint> points = new ArrayList<>(); DescriptiveStatistics stats = new DescriptiveStatistics(); Distribution distribution = null;//from www . ja v a 2 s. c o m for (int i = 0; i < 100; ++i) { double val = r.nextDouble() * 1000; DataPoint dp = (new DataPoint(i, val, null, "foo")); points.add(dp); stats.addValue(val); if (distribution == null) { distribution = new Distribution(dp, ScalingFunctions.NONE, new GlobalStatistics()); } else { distribution.addDataPoint(dp, ScalingFunctions.NONE); } } double realMedian = stats.getPercentile(50); double approxMedian = distribution.getMedian(); System.out.println("mean and std dev: " + stats.getMean() + ", " + Math.sqrt(stats.getVariance())); System.out.println("Real : " + realMedian + ", approx: " + approxMedian); Assert.assertTrue(Math.abs(realMedian - approxMedian) < 5); }
From source file:it.stilo.g.util.WeightedZIPFianGenerator.java
public void run() { Random rnd = new Random(System.currentTimeMillis()); for (int i = chunk; i < g.out.length; i += runner) { for (int j = 0; j < degree; j++) { while (!g.testAndAdd(i, dist.sample() - 1, rnd.nextDouble())) ;/*from w w w.ja v a 2 s .c om*/ } } barrier.countDown(); }
From source file:org.geomesa.QuickStart.java
static FeatureCollection createNewFeatures(SimpleFeatureType simpleFeatureType, int numNewFeatures) { DefaultFeatureCollection featureCollection = new DefaultFeatureCollection(); String id;// www . j a v a 2 s. co 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$.MODULE$.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 // accumulate this new feature in the collection featureCollection.add(simpleFeature); } return featureCollection; }
From source file:org.orekit.utils.FieldAngularCoordinatesTest.java
private FieldVector3D<DerivativeStructure> randomVector(Random random, double norm) { double n = random.nextDouble() * norm; double x = random.nextDouble(); double y = random.nextDouble(); double z = random.nextDouble(); return new FieldVector3D<DerivativeStructure>(n, createVector(x, y, z, 4).normalize()); }
From source file:edu.berkeley.sparrow.examples.HeterogeneousFrontend.java
/** * Generates exponentially distributed interarrival delays. *//*from w w w. j av a 2s. c om*/ public double generateInterarrivalDelay(Random r, double lambda) { double u = r.nextDouble(); return -Math.log(u) / lambda; }
From source file:pl.mg6.newmaps.demo.AddMarkersInBackgroundExampleActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.many_markers_example); map = GoogleMapHelper.getMap(this, R.id.many_markers_map); Random r = new Random(); long start = SystemClock.uptimeMillis(); for (int i = 0; i < ManyMarkersExampleActivity.MARKERS_COUNT; i++) { LatLng position = new LatLng((r.nextDouble() - 0.5) * 170.0, (r.nextDouble() - 0.5) * 360.0); positions[i] = new LatLngDistance(position); }/*w ww .j av a 2s . c o m*/ long end = SystemClock.uptimeMillis(); Log.w(TAG, "generate time: " + (end - start)); }