List of usage examples for java.util Random nextDouble
public double nextDouble()
From source file:org.apache.kylin.measure.percentile.PercentileCounterTest.java
@Test public void testTDigest() { double compression = 100; double quantile = 0.5; PercentileCounter counter = new PercentileCounter(compression, quantile); TDigest tDigest = TDigest.createAvlTreeDigest(compression); Random random = new Random(); int dataSize = 10000; List<Double> dataset = Lists.newArrayListWithCapacity(dataSize); for (int i = 0; i < dataSize; i++) { double d = random.nextDouble(); counter.add(d);/*from www . ja v a 2 s. com*/ tDigest.add(d); } double actualResult = counter.getResultEstimate(); Collections.sort(dataset); double expectedResult = tDigest.quantile(quantile); assertEquals(expectedResult, actualResult, 0); }
From source file:pl.mg6.android.maps.extensions.demo.LaunchTimeTestActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.launch_time_test); FragmentManager fm = getSupportFragmentManager(); SupportMapFragment f = (SupportMapFragment) fm.findFragmentById(R.id.map); GoogleMap map = f.getExtendedMap();/*from w w w . j a va 2s. co m*/ int clusteringType = getIntent().getIntExtra(EXTRA_CLUSTERING_TYPE, CLUSTERING_DISABLED); ClusteringSettings settings = new ClusteringSettings(); switch (clusteringType) { case CLUSTERING_DISABLED_DYNAMIC: settings.enabled(false).addMarkersDynamically(true); break; case CLUSTERING_ENABLED: settings.clusterOptionsProvider(new DemoClusterOptionsProvider(getResources())); break; case CLUSTERING_ENABLED_DYNAMIC: settings.clusterOptionsProvider(new DemoClusterOptionsProvider(getResources())) .addMarkersDynamically(true); break; default: settings.enabled(false); } map.setClustering(settings); Random r = new Random(0); MarkerOptions options = new MarkerOptions(); long start = SystemClock.uptimeMillis(); for (int i = 0; i < MARKERS_COUNT; i++) { LatLng position = new LatLng(r.nextDouble() * 170 - 85, r.nextDouble() * 360 - 180); map.addMarker(options.position(position)); } long end = SystemClock.uptimeMillis(); long time = end - start; float zoom = map.getCameraPosition().zoom; String format = "Time adding %d markers (option: %d, zoom: %.1f): %d"; String text = String.format(Locale.US, format, MARKERS_COUNT, clusteringType, zoom, time); Log.i(TAG, text); }
From source file:com.comphenix.xp.SampleRange.java
private int sampleIntReduced(Random rnd) { double value = sampleDouble(rnd); // Probability of adding the fraction double fraction = value - Math.floor(value); double toAdd = rnd.nextDouble() < fraction ? Math.signum(value) : 0; return (int) value + (int) toAdd; }
From source file:ffx.utilities.BlockAverager.java
/** * Uncorrelated process: x = 5 + 2 * rand(N,1) - 1 Correlated process: x(1) * = 0; x(t+1) = 0.95 * x(t) + 2 * rand(N,1) - 1; shift all up by 5 *///from w w w . ja v a 2s .c om public static void generateTestData(String filename, int size) throws IOException { logger.info(format(" Generating test data set of size: %d.", size)); File outFile = new File(filename); if (outFile.exists()) { logger.severe("Outfile already exists."); } Random rng = new Random(); double[] uncor = new double[size]; double[] corr = new double[size]; corr[0] = 0.0; for (int i = 0; i < size; i++) { uncor[i] = 5 + (2 * rng.nextDouble() - 1); if (i > 0) { corr[i] = 0.95 * corr[i - 1] + (2 * rng.nextDouble() - 1); } } for (int i = 0; i < size; i++) { corr[i] += 5.0; } BufferedWriter bw = new BufferedWriter(new FileWriter(outFile)); for (int i = 0; i < size; i++) { bw.write(format(" %4d %6.4f %6.4f", i, uncor[i], corr[i])); bw.newLine(); } bw.close(); logger.info(format(" Data saved to: %s", filename)); }
From source file:com.insightml.data.samples.AbstractSamples.java
@Override public final Pair<ISamples<S, E>, ISamples<S, E>> sample(final double ratio, final Random random) { Check.num(ratio, 0.01, 0.95);/*from w w w . ja v a 2 s . com*/ final int n = size(); if (featureNames() == null) { final List<S> list = new LinkedList<>(); for (int i = 0; i < n; ++i) { if (random.nextDouble() <= ratio) { list.add(get(i)); } } return new Pair<>(new Samples<>(list), null); } final IntArray list = new IntArray(n); for (int i = 0; i < n; ++i) { if (random.nextDouble() <= ratio) { list.add(i); } } return new Pair<>(subset(list.toArray()), null); }
From source file:co.com.estacionsannicolas.initializer.SeedDataInitializer.java
private Date getRandomBirthDate() { long seventyYearsInMillis = 70L * 365L * 24L * 60L * 60L * 1000L; Random random = new Random(); long randomYearOffsetInMillis = (long) (random.nextDouble() * seventyYearsInMillis); return new Date(System.currentTimeMillis() - randomYearOffsetInMillis); }
From source file:com.polytech4A.cuttingstock.core.solver.SimulatedAnnealing.java
/** * Simulate Annealing Algorithm.//from ww w . j av a2s.c om * * @param solution First Solution of the Algorithm, it's also best solution for beginning. * @return bestsolution Found. */ public Solution simulateAnnealing(Solution solution) { Solution bestSolution = solution; Result bestCost = simplex.minimize(solution); Solution randomSolution; Result randomSolutionCost; Solution currentSolution = solution; Result currentCost = bestCost.clone(); int step = (int) nbIterations / 100; int progress = 10; logger.info("First Cost = " + bestCost); double deltaF; for (int j = 0; j < 100; j++) { for (int i = 0; i < step; i++) { randomSolution = generateBetterNeighbour(currentSolution); randomSolutionCost = randomSolution.getCost(); deltaF = currentCost.getCost() - randomSolutionCost.getCost(); if (deltaF >= 0) { currentSolution = randomSolution; currentCost = randomSolutionCost; if (currentCost.getCost() < bestCost.getCost()) { bestSolution = currentSolution; bestCost = currentCost; } } else { Random random = new Random(System.currentTimeMillis()); double p = random.nextDouble(); double exp = FastMath.exp((-deltaF) / temperature); if (p <= exp) { currentSolution = randomSolution; currentCost = randomSolutionCost; } } } temperature *= mu; if (j % progress == 0) { int pourcent = j / progress; pourcent *= 10; StringBuffer stbf = new StringBuffer(); stbf.append("CSV;").append(temperature).append(";temperature;").append(bestCost.getCost()) .append(";bestcost;").append(pourcent).append("%..."); logger.info(stbf.toString()); } } logger.info("Best Cost :" + bestCost); return bestSolution; }
From source file:org.apache.kylin.measure.percentile.PercentileCounterTest.java
@Test public void testBasic() { int times = 1; int compression = 100; for (int t = 0; t < times; t++) { PercentileCounter counter = new PercentileCounter(compression, 0.5); Random random = new Random(); int dataSize = 10000; List<Double> dataset = Lists.newArrayListWithCapacity(dataSize); for (int i = 0; i < dataSize; i++) { double d = random.nextDouble(); counter.add(d);//from w w w.j ava2 s . c o m dataset.add(d); } Collections.sort(dataset); double actualResult = counter.getResultEstimate(); double expectedResult = MathUtil.findMedianInSortedList(dataset); assertEquals(expectedResult, actualResult, 0.001); } }
From source file:org.orekit.frames.FrameTest.java
private void checkNoTransform(Transform transform, Random random) { for (int i = 0; i < 100; ++i) { Vector3D a = new Vector3D(random.nextDouble(), random.nextDouble(), random.nextDouble()); Vector3D b = transform.transformVector(a); Assert.assertEquals(0, a.subtract(b).getNorm(), 1.0e-10); Vector3D c = transform.transformPosition(a); Assert.assertEquals(0, a.subtract(c).getNorm(), 1.0e-10); }/* w w w. j ava2 s. c o m*/ }
From source file:eu.amidst.core.exponentialfamily.EF_SparseDirichlet.java
/** * {@inheritDoc}//from ww w . j a v a 2 s . com */ @Override public EF_UnivariateDistribution randomInitialization(Random random) { for (int i = 0; i < this.nOfStates; i++) { this.getNaturalParameters().set(i, 5 * random.nextDouble() + 1 + 1e-5); } fixNumericalInstability(); this.updateMomentFromNaturalParameters(); return this; }