List of usage examples for java.util Random nextDouble
public double nextDouble()
From source file:org.latticesoft.util.common.NumeralUtil.java
/** * Generates next random double/* w w w. j a va2 s . c o m*/ * @param start * @return the generated double */ public static double getRandomDouble(double start, double end) { Random rand = new Random(System.currentTimeMillis()); double d = rand.nextDouble(); if (end > start) { d = d * (end - start) + start; } return d; }
From source file:org.broad.igv.gwas.GWASParser.java
public static void generateUnsortedGWASData() throws Exception { Random random = new Random(12345); int numFeats = 100; String headerLine = StringUtils.join(new String[] { "Chr", "bp", "p", "snp" }, "\t"); PrintWriter writer = new PrintWriter("random.gwas"); writer.println(headerLine);/*from w ww.ja va2 s . co m*/ for (int ii = 0; ii < numFeats; ii++) { String chr = String.format("chr%d", random.nextInt(20)); String location = String.format("%d", random.nextInt(Integer.MAX_VALUE)); String pVal = String.format("%2.8f", random.nextDouble() / 1000.0d); String rsID = String.format("rs%d", random.nextInt(100000)); String line = StringUtils.join(new String[] { chr, location, pVal, rsID }, "\t"); writer.println(line); } writer.flush(); writer.close(); }
From source file:org.apache.mahout.clustering.lda.LDADriver.java
private static void writeInitialState(Path statePath, int numTopics, int numWords) throws IOException { Configuration job = new Configuration(); FileSystem fs = statePath.getFileSystem(job); DoubleWritable v = new DoubleWritable(); Random random = RandomUtils.getRandom(); for (int k = 0; k < numTopics; ++k) { Path path = new Path(statePath, "part-" + k); SequenceFile.Writer writer = new SequenceFile.Writer(fs, job, path, IntPairWritable.class, DoubleWritable.class); double total = 0.0; // total number of pseudo counts we made for (int w = 0; w < numWords; ++w) { IntPairWritable kw = new IntPairWritable(k, w); // A small amount of random noise, minimized by having a floor. double pseudocount = random.nextDouble() + 1.0E-8; total += pseudocount;/*from w w w. java 2 s .c o m*/ v.set(Math.log(pseudocount)); writer.append(kw, v); } IntPairWritable kTsk = new IntPairWritable(k, TOPIC_SUM_KEY); v.set(Math.log(total)); writer.append(kTsk, v); writer.close(); } }
From source file:org.brekka.stillingar.example.FieldTypesDOMTest.java
private static Testing writeConfig() { Random r = new Random(); ConfigurationDocument doc = ConfigurationDocument.Factory.newInstance(); Configuration newConfiguration = doc.addNewConfiguration(); FeatureFlagType featureFlag = newConfiguration.addNewFeatureFlag(); featureFlag.setKey("turbo"); featureFlag.setBooleanValue(true);//w w w .ja v a 2s . co m Testing testing = newConfiguration.addNewTesting(); testing.setAnyURI("http://brekka.org/" + RandomStringUtils.randomAlphanumeric(10)); testing.setBoolean(r.nextBoolean()); testing.setByte((byte) r.nextInt()); Calendar cal = Calendar.getInstance(); testing.setDate(cal); testing.setDateTime(cal); testing.setDecimal(BigDecimal.valueOf(r.nextDouble())); testing.setDouble(r.nextDouble()); testing.setFloat(r.nextFloat()); testing.setInt(r.nextInt()); testing.setInteger(BigInteger.valueOf(r.nextLong())); testing.setLanguage("en"); testing.setLong(r.nextLong()); testing.setShort((short) r.nextInt()); testing.setString(RandomStringUtils.randomAlphanumeric(24)); testing.setTime(cal); testing.setUUID(UUID.randomUUID().toString()); testing.setPeriod(new GDuration("P5Y2M10DT15H")); byte[] binary = new byte[32]; r.nextBytes(binary); testing.setBinary(binary); TestSupport.write(doc); return testing; }
From source file:isl.FIMS.utils.Utils.java
public static String generateRandomPassword() { // Pick from some letters that won't be easily mistaken for each // other. So, for example, omit o O and 0, 1 l and L. String letters = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789+@"; String pw = ""; for (int i = 0; i < PASSWORD_LENGTH; i++) { Random r = new Random(); int index = (int) (r.nextDouble() * letters.length()); pw += letters.substring(index, index + 1); }//from w ww . j a v a 2s .c o m return pw; }
From source file:org.locationtech.geomesa.examples.KafkaQuickStart.java
public static void addSimpleFeatures(SimpleFeatureType sft, FeatureStore producerFS) throws InterruptedException, IOException { final int MIN_X = -180; final int MAX_X = 180; final int MIN_Y = -90; final int MAX_Y = 90; final int DX = 2; final int DY = 1; final String[] PEOPLE_NAMES = { "James", "John", "Peter", "Hannah", "Claire", "Gabriel" }; final long SECONDS_PER_YEAR = 365L * 24L * 60L * 60L; final Random random = new Random(); final DateTime MIN_DATE = new DateTime(2015, 1, 1, 0, 0, 0, DateTimeZone.forID("UTC")); SimpleFeatureBuilder builder = new SimpleFeatureBuilder(sft); DefaultFeatureCollection featureCollection = new DefaultFeatureCollection(); // creates and updates two SimpleFeatures. // the first time this for loop runs the two SimpleFeatures are created. // in the subsequent iterations of the for loop, the two SimpleFeatures are updated. int numFeatures = (MAX_X - MIN_X) / DX; for (int i = 1; i <= numFeatures; i++) { builder.add(PEOPLE_NAMES[i % PEOPLE_NAMES.length]); // name builder.add((int) Math.round(random.nextDouble() * 110)); // age builder.add(MIN_DATE.plusSeconds((int) Math.round(random.nextDouble() * SECONDS_PER_YEAR)).toDate()); // dtg builder.add(WKTUtils$.MODULE$.read("POINT(" + (MIN_X + DX * i) + " " + (MIN_Y + DY * i) + ")")); // geom SimpleFeature feature1 = builder.buildFeature("1"); builder.add(PEOPLE_NAMES[(i + 1) % PEOPLE_NAMES.length]); // name builder.add((int) Math.round(random.nextDouble() * 110)); // age builder.add(MIN_DATE.plusSeconds((int) Math.round(random.nextDouble() * SECONDS_PER_YEAR)).toDate()); // dtg builder.add(WKTUtils$.MODULE$.read("POINT(" + (MIN_X + DX * i) + " " + (MAX_Y - DY * i) + ")")); // geom SimpleFeature feature2 = builder.buildFeature("2"); // write the SimpleFeatures to Kafka featureCollection.add(feature1); featureCollection.add(feature2); producerFS.addFeatures(featureCollection); featureCollection.clear();/*from www.jav a 2 s.c o m*/ // wait 200 ms in between updating SimpleFeatures to simulate a stream of data Thread.sleep(200); } }
From source file:com.asakusafw.runtime.stage.input.DefaultSplitCombiner.java
private static void initializeGene(Environment env, Gene gene) { assert env != null; assert gene != null; Random random = env.random; int[] schema = gene.schema; for (int i = 0; i < schema.length; i++) { if (random.nextDouble() < env.locality) { schema[i] = env.getRandomLocalSlot(env.splits[i]); } else {// www . ja v a2s .c o m schema[i] = env.getRandomSlot(); } assert 0 <= schema[i] && schema[i] < env.slots.length; } gene.eval(); }
From source file:lirmm.inria.fr.math.BigSparseRealMatrix.java
/** * Generate a sparse matrix of random values. * * @param rowDimension Number of rows of the matrix. * @param columnDimension Number of columns of the matrix. * @param sparsity Percentage of data in the matrix. * @return An instance of BigSparseRealMatrix. *///from w w w. j a va2 s . c o m public static BigSparseRealMatrix randomGenerateMatrix(int rowDimension, int columnDimension, double sparsity) { BigSparseRealMatrix m = new BigSparseRealMatrix(rowDimension, columnDimension); long lrow = (long) rowDimension; long lcolumn = (long) columnDimension; long total = (long) (lrow * lcolumn * sparsity / 100); Random r = new Random(); for (long i = 0; i < total; i++) { int row = r.nextInt(rowDimension); int col = r.nextInt(columnDimension); while (m.getEntry(row, col) != 0) { row = r.nextInt(rowDimension); col = r.nextInt(columnDimension); } m.setEntry(row, col, r.nextDouble()); } return m; }
From source file:com.example.geomesa.kafka.KafkaQuickStart.java
public static void addSimpleFeatures(SimpleFeatureType sft, FeatureStore producerFS, String visibility) throws InterruptedException, IOException { final int MIN_X = -180; final int MAX_X = 180; final int MIN_Y = -90; final int MAX_Y = 90; final int DX = 2; final int DY = 1; final String[] PEOPLE_NAMES = { "James", "John", "Peter", "Hannah", "Claire", "Gabriel" }; final long SECONDS_PER_YEAR = 365L * 24L * 60L * 60L; final Random random = new Random(); final ZonedDateTime MIN_DATE = ZonedDateTime.of(2015, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC); SimpleFeatureBuilder builder = new SimpleFeatureBuilder(sft); DefaultFeatureCollection featureCollection = new DefaultFeatureCollection(); // creates and updates two SimpleFeatures. // the first time this for loop runs the two SimpleFeatures are created. // in the subsequent iterations of the for loop, the two SimpleFeatures are updated. int numFeatures = (MAX_X - MIN_X) / DX; for (int i = 1; i <= numFeatures; i++) { builder.add(PEOPLE_NAMES[i % PEOPLE_NAMES.length]); // name builder.add((int) Math.round(random.nextDouble() * 110)); // age builder.add(Date.from(// ww w . java 2s .c om MIN_DATE.plusSeconds((int) Math.round(random.nextDouble() * SECONDS_PER_YEAR)).toInstant())); // dtg builder.add(WKTUtils$.MODULE$.read("POINT(" + (MIN_X + DX * i) + " " + (MIN_Y + DY * i) + ")")); // geom SimpleFeature feature1 = builder.buildFeature("1"); feature1.getUserData().put(Hints.USE_PROVIDED_FID, Boolean.TRUE); builder.add(PEOPLE_NAMES[(i + 1) % PEOPLE_NAMES.length]); // name builder.add((int) Math.round(random.nextDouble() * 110)); // age builder.add(Date.from( MIN_DATE.plusSeconds((int) Math.round(random.nextDouble() * SECONDS_PER_YEAR)).toInstant())); // dtg builder.add(WKTUtils$.MODULE$.read("POINT(" + (MIN_X + DX * i) + " " + (MAX_Y - DY * i) + ")")); // geom SimpleFeature feature2 = builder.buildFeature("2"); feature2.getUserData().put(Hints.USE_PROVIDED_FID, Boolean.TRUE); if (visibility != null) { feature1.getUserData().put("geomesa.feature.visibility", visibility); feature2.getUserData().put("geomesa.feature.visibility", visibility); } // write the SimpleFeatures to Kafka featureCollection.add(feature1); featureCollection.add(feature2); producerFS.addFeatures(featureCollection); featureCollection.clear(); // wait 100 ms in between updating SimpleFeatures to simulate a stream of data Thread.sleep(100); } }
From source file:com.example.geomesa.kafka08.KafkaQuickStart.java
public static void addSimpleFeatures(SimpleFeatureType sft, FeatureStore producerFS, String visibility) throws InterruptedException, IOException { final int MIN_X = -180; final int MAX_X = 180; final int MIN_Y = -90; final int MAX_Y = 90; final int DX = 2; final int DY = 1; final String[] PEOPLE_NAMES = { "James", "John", "Peter", "Hannah", "Claire", "Gabriel" }; final long SECONDS_PER_YEAR = 365L * 24L * 60L * 60L; final Random random = new Random(); final DateTime MIN_DATE = new DateTime(2015, 1, 1, 0, 0, 0, DateTimeZone.forID("UTC")); SimpleFeatureBuilder builder = new SimpleFeatureBuilder(sft); DefaultFeatureCollection featureCollection = new DefaultFeatureCollection(); // creates and updates two SimpleFeatures. // the first time this for loop runs the two SimpleFeatures are created. // in the subsequent iterations of the for loop, the two SimpleFeatures are updated. int numFeatures = (MAX_X - MIN_X) / DX; for (int i = 1; i <= numFeatures; i++) { builder.add(PEOPLE_NAMES[i % PEOPLE_NAMES.length]); // name builder.add((int) Math.round(random.nextDouble() * 110)); // age builder.add(MIN_DATE.plusSeconds((int) Math.round(random.nextDouble() * SECONDS_PER_YEAR)).toDate()); // dtg builder.add(WKTUtils$.MODULE$.read("POINT(" + (MIN_X + DX * i) + " " + (MIN_Y + DY * i) + ")")); // geom SimpleFeature feature1 = builder.buildFeature("1"); builder.add(PEOPLE_NAMES[(i + 1) % PEOPLE_NAMES.length]); // name builder.add((int) Math.round(random.nextDouble() * 110)); // age builder.add(MIN_DATE.plusSeconds((int) Math.round(random.nextDouble() * SECONDS_PER_YEAR)).toDate()); // dtg builder.add(WKTUtils$.MODULE$.read("POINT(" + (MIN_X + DX * i) + " " + (MAX_Y - DY * i) + ")")); // geom SimpleFeature feature2 = builder.buildFeature("2"); if (visibility != null) { feature1.getUserData().put("geomesa.feature.visibility", visibility); feature2.getUserData().put("geomesa.feature.visibility", visibility); }//from w w w . j a va 2 s. c om // write the SimpleFeatures to Kafka featureCollection.add(feature1); featureCollection.add(feature2); producerFS.addFeatures(featureCollection); featureCollection.clear(); // wait 100 ms in between updating SimpleFeatures to simulate a stream of data Thread.sleep(100); } }