List of usage examples for java.util Random nextDouble
public double nextDouble()
From source file:org.apache.kylin.storage.hbase.util.HbaseStreamingInput.java
public static void randomScan(String tableName) throws IOException { final Semaphore semaphore = new Semaphore(0); new Thread(new Runnable() { @Override//w ww . j a v a 2 s . c o m public void run() { scheduleJob(semaphore, 60000);//1 minutes a batch } }).start(); while (true) { try { semaphore.acquire(); int waiting = semaphore.drainPermits(); if (waiting > 0) { logger.warn("Too many queries to handle! Blocking " + waiting + " sets of scan requests"); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); e.printStackTrace(); } Random r = new Random(); Connection conn = getConnection(); Table table = conn.getTable(TableName.valueOf(tableName)); long leftBound = getFirstKeyTime(table); long rightBound = System.currentTimeMillis(); for (int t = 0; t < 5; ++t) { long start = (long) (leftBound + r.nextDouble() * (rightBound - leftBound)); long end = start + 600000;//a period of 10 minutes logger.info("A scan from " + formatTime(start) + " to " + formatTime(end)); Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes(start)); scan.setStopRow(Bytes.toBytes(end)); scan.addFamily(CF); ResultScanner scanner = table.getScanner(scan); long hash = 0; int rowCount = 0; for (Result result : scanner) { Cell cell = result.getColumnLatestCell(CF, QN); byte[] value = cell.getValueArray(); if (cell.getValueLength() != CELL_SIZE) { logger.error("value size invalid!!!!!"); } hash += Arrays.hashCode(Arrays.copyOfRange(value, cell.getValueOffset(), cell.getValueLength() + cell.getValueOffset())); rowCount++; } scanner.close(); logger.info("Scanned " + rowCount + " rows, the (meaningless) hash for the scan is " + hash); } table.close(); conn.close(); } }
From source file:org.deeplearning4j.examples.multigpu.video.VideoGenerator.java
private static int[] generateVideo(String path, int nFrames, int width, int height, int numShapes, Random r, boolean backgroundNoise, int numDistractorsPerFrame) throws Exception { //First: decide where transitions between one shape and another are double[] rns = new double[numShapes]; double sum = 0; for (int i = 0; i < numShapes; i++) { rns[i] = r.nextDouble(); sum += rns[i];//from w w w .j a va2 s . c o m } for (int i = 0; i < numShapes; i++) rns[i] /= sum; int[] startFrames = new int[numShapes]; startFrames[0] = 0; for (int i = 1; i < numShapes; i++) { startFrames[i] = (int) (startFrames[i - 1] + MIN_FRAMES + rns[i] * (nFrames - numShapes * MIN_FRAMES)); } //Randomly generate shape positions, velocities, colors, and type int[] shapeTypes = new int[numShapes]; int[] initialX = new int[numShapes]; int[] initialY = new int[numShapes]; double[] velocityX = new double[numShapes]; double[] velocityY = new double[numShapes]; Color[] color = new Color[numShapes]; for (int i = 0; i < numShapes; i++) { shapeTypes[i] = r.nextInt(NUM_SHAPES); initialX[i] = SHAPE_MIN_DIST_FROM_EDGE + r.nextInt(width - SHAPE_SIZE - 2 * SHAPE_MIN_DIST_FROM_EDGE); initialY[i] = SHAPE_MIN_DIST_FROM_EDGE + r.nextInt(height - SHAPE_SIZE - 2 * SHAPE_MIN_DIST_FROM_EDGE); velocityX[i] = -1 + 2 * r.nextDouble(); velocityY[i] = -1 + 2 * r.nextDouble(); color[i] = new Color(r.nextFloat(), r.nextFloat(), r.nextFloat()); } //Generate a sequence of BufferedImages with the given shapes, and write them to the video SequenceEncoder enc = new SequenceEncoder(new File(path)); int currShape = 0; int[] labels = new int[nFrames]; for (int i = 0; i < nFrames; i++) { if (currShape < numShapes - 1 && i >= startFrames[currShape + 1]) currShape++; BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = bi.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setBackground(Color.BLACK); if (backgroundNoise) { for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bi.setRGB(x, y, new Color(r.nextFloat() * MAX_NOISE_VALUE, r.nextFloat() * MAX_NOISE_VALUE, r.nextFloat() * MAX_NOISE_VALUE).getRGB()); } } } g2d.setColor(color[currShape]); //Position of shape this frame int currX = (int) (initialX[currShape] + (i - startFrames[currShape]) * velocityX[currShape] * MAX_VELOCITY); int currY = (int) (initialY[currShape] + (i - startFrames[currShape]) * velocityY[currShape] * MAX_VELOCITY); //Render the shape switch (shapeTypes[currShape]) { case 0: //Circle g2d.fill(new Ellipse2D.Double(currX, currY, SHAPE_SIZE, SHAPE_SIZE)); break; case 1: //Square g2d.fill(new Rectangle2D.Double(currX, currY, SHAPE_SIZE, SHAPE_SIZE)); break; case 2: //Arc g2d.fill(new Arc2D.Double(currX, currY, SHAPE_SIZE, SHAPE_SIZE, 315, 225, Arc2D.PIE)); break; case 3: //Line g2d.setStroke(lineStroke); g2d.draw(new Line2D.Double(currX, currY, currX + SHAPE_SIZE, currY + SHAPE_SIZE)); break; default: throw new RuntimeException(); } //Add some distractor shapes, which are present for one frame only for (int j = 0; j < numDistractorsPerFrame; j++) { int distractorShapeIdx = r.nextInt(NUM_SHAPES); int distractorX = DISTRACTOR_MIN_DIST_FROM_EDGE + r.nextInt(width - SHAPE_SIZE); int distractorY = DISTRACTOR_MIN_DIST_FROM_EDGE + r.nextInt(height - SHAPE_SIZE); g2d.setColor(new Color(r.nextFloat(), r.nextFloat(), r.nextFloat())); switch (distractorShapeIdx) { case 0: g2d.fill(new Ellipse2D.Double(distractorX, distractorY, SHAPE_SIZE, SHAPE_SIZE)); break; case 1: g2d.fill(new Rectangle2D.Double(distractorX, distractorY, SHAPE_SIZE, SHAPE_SIZE)); break; case 2: g2d.fill(new Arc2D.Double(distractorX, distractorY, SHAPE_SIZE, SHAPE_SIZE, 315, 225, Arc2D.PIE)); break; case 3: g2d.setStroke(lineStroke); g2d.draw(new Line2D.Double(distractorX, distractorY, distractorX + SHAPE_SIZE, distractorY + SHAPE_SIZE)); break; default: throw new RuntimeException(); } } enc.encodeImage(bi); g2d.dispose(); labels[i] = shapeTypes[currShape]; } enc.finish(); //write .mp4 return labels; }
From source file:org.apache.hyracks.storage.am.btree.OrderedIndexTestUtils.java
public static String getRandomString(int length, Random rnd) { String s = Long.toHexString(Double.doubleToLongBits(rnd.nextDouble())); StringBuilder strBuilder = new StringBuilder(); for (int i = 0; i < s.length() && i < length; i++) { strBuilder.append(s.charAt(Math.abs(rnd.nextInt()) % s.length())); }/*from w ww .ja v a 2 s . c o m*/ return strBuilder.toString(); }
From source file:org.apache.mahout.classifier.df.data.Utils.java
/** * generates a random vector based on the given attributes.<br> * the attributes' values are generated as follows :<br> * <ul>/*from w w w . j a va 2s . c om*/ * <li>each IGNORED attribute receives a Double.NaN</li> * <li>each NUMERICAL attribute receives a random double</li> * <li>each CATEGORICAL and LABEL attribute receives a random integer in the * range [0, CATEGORICAL_RANGE[</li> * </ul> * * @param attrs attributes description */ private static double[] randomVector(Random rng, Attribute[] attrs, boolean regression) { double[] vector = new double[attrs.length]; for (int attr = 0; attr < attrs.length; attr++) { if (attrs[attr].isIgnored()) { vector[attr] = Double.NaN; } else if (attrs[attr].isNumerical()) { vector[attr] = rng.nextDouble(); } else if (attrs[attr].isCategorical()) { vector[attr] = rng.nextInt(CATEGORICAL_RANGE); } else { // LABEL if (regression) { vector[attr] = rng.nextDouble(); } else { vector[attr] = rng.nextInt(CATEGORICAL_RANGE); } } } return vector; }
From source file:enterprayz.megatools.Tools.java
public static int getRandomInteger(int aStart, int aEnd, Random aRandom) { if (aStart > aEnd) { throw new IllegalArgumentException("Start cannot exceed End."); }// w w w .java 2 s . c o m //get the range, casting to long to avoid overflow problems long range = (long) aEnd - (long) aStart + 1; // compute a fraction of the range, 0 <= frac < range long fraction = (long) (range * aRandom.nextDouble()); return (int) (fraction + aStart); }
From source file:edu.iu.kmeans.regroupallgather.KMUtil.java
/** * Generate centroids and upload to the cDir * /* w w w .jav a 2 s . c om*/ * @param numCentroids * @param vectorSize * @param configuration * @param random * @param cenDir * @param fs * @throws IOException */ static void generateCentroids(int numCentroids, int vectorSize, Configuration configuration, Path cenDir, FileSystem fs) throws IOException { Random random = new Random(); double[] data = null; if (fs.exists(cenDir)) fs.delete(cenDir, true); if (!fs.mkdirs(cenDir)) { throw new IOException("Mkdirs failed to create " + cenDir.toString()); } data = new double[numCentroids * vectorSize]; for (int i = 0; i < data.length; i++) { // data[i] = 1000; data[i] = random.nextDouble() * 1000; } Path initClustersFile = new Path(cenDir, Constants.CENTROID_FILE_NAME); System.out.println("Generate centroid data." + initClustersFile.toString()); FSDataOutputStream out = fs.create(initClustersFile, true); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out)); for (int i = 0; i < data.length; i++) { if ((i % vectorSize) == (vectorSize - 1)) { bw.write(data[i] + ""); bw.newLine(); } else { bw.write(data[i] + " "); } } bw.flush(); bw.close(); System.out.println("Wrote centroids data to file"); }
From source file:com.mapr.synth.drive.Car.java
/** * Chooses whether to plan a "highway" or "local" segment based on the distance to be traveled. * * @return True if this should be a highway segment. *//* w ww . j a va 2 s .c o m*/ private static boolean pickHighway(double distance, Random rand) { // formula was picked heuristically to fit intuition // d(km) p // 1 0.002472623 // 2 0.013828044 // 5 0.121702566 // 10 0.439414832 // 20 0.815977791 // 50 0.977687816 // 100 0.995981922 double logOdds = -6 + 2 * log(distance); double u = rand.nextDouble(); return log(u / (1 - u)) < logOdds; }
From source file:edu.iu.daal_kmeans.regroupallgather.KMUtil.java
/** * Generate centroids and upload to the cDir * /*from w ww . ja v a 2 s . co m*/ * @param numCentroids * @param vectorSize * @param configuration * @param random * @param cenDir * @param fs * @throws IOException */ static void generateCentroids(int numCentroids, int vectorSize, Configuration configuration, Path cenDir, FileSystem fs) throws IOException { Random random = new Random(); double[] data = null; if (fs.exists(cenDir)) fs.delete(cenDir, true); if (!fs.mkdirs(cenDir)) { throw new IOException("Mkdirs failed to create " + cenDir.toString()); } data = new double[numCentroids * vectorSize]; for (int i = 0; i < data.length; i++) { data[i] = random.nextDouble() * 1000; } Path initClustersFile = new Path(cenDir, Constants.CENTROID_FILE_NAME); System.out.println("Generate centroid data." + initClustersFile.toString()); FSDataOutputStream out = fs.create(initClustersFile, true); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out)); for (int i = 0; i < data.length; i++) { if ((i % vectorSize) == (vectorSize - 1)) { bw.write(data[i] + ""); bw.newLine(); } else { bw.write(data[i] + " "); } } bw.flush(); bw.close(); System.out.println("Wrote centroids data to file"); }
From source file:net.sf.javaml.tools.DatasetTools.java
/** * Creates a random instance using the//from w ww . ja v a 2 s. c o m * {@link DatasetTools#getMinMax(Dataset)} method. The random instance is * created by choosing a random value for each attribute (which is between * the min and max value of the attribute). * * @param data * the data set * @return a random instance */ public static double[] getRandomInstance(Dataset data, Random r) { final int MIN_INDEX = 0; final int MAX_INDEX = 1; double[][] minMax = getMinMax(data); final int noAttributes = minMax.length; double[] ret = new double[noAttributes]; for (int a = 0; a < noAttributes; a++) { ret[a] = minMax[a][MIN_INDEX] + (minMax[a][MAX_INDEX] - minMax[a][MIN_INDEX]) * r.nextDouble(); } return ret; }
From source file:org.apache.mahout.df.data.Utils.java
/** * generates a random vector based on the given attributes.<br> * the attributes' values are generated as follows :<br> * <ul>/* w w w . j a v a 2 s.c om*/ * <li>each IGNORED attribute receives a Double.NaN</li> * <li>each NUMERICAL attribute receives a random double</li> * <li>each CATEGORICAL and LABEL attribute receives a random integer in the * range [0, CATEGORICAL_RANGE[</li> * </ul> * * @param rng * @param attrs attributes description * @return */ private static double[] randomVector(Random rng, Attribute[] attrs) { double[] vector = new double[attrs.length]; for (int attr = 0; attr < attrs.length; attr++) { if (attrs[attr].isIgnored()) { vector[attr] = Double.NaN; } else if (attrs[attr].isNumerical()) { vector[attr] = rng.nextDouble(); } else { // CATEGORICAL or LABEL vector[attr] = rng.nextInt(CATEGORICAL_RANGE); } } return vector; }