List of usage examples for java.util Random nextDouble
public double nextDouble()
From source file:de.tum.bgu.msm.syntheticPopulationGenerator.kagawa.SyntheticPopJP.java
private static int obtainDriverLicense(int gender, int age, TableDataSet prob) { //assign if the person holds a driver license based on the probabilities obtained from MiD data int license = 0; int finish = 0; int row = 1;//from w ww. jav a 2s. c om int threshold = 0; if (age > 18) { while (finish == 0 & row < prob.getRowCount()) { if (age > prob.getValueAt(row, "ageLimit")) { row++; } else { finish = 1; } } if (finish == 0) { row = prob.getRowCount(); } if (gender == 0) { threshold = (int) prob.getValueAt(row, "male"); } else { threshold = (int) prob.getValueAt(row, "female"); } Random rn = new Random(); if (rn.nextDouble() * 100 < threshold) { license = 1; } } //if they are younger than 18, they don't hold driver license return license; }
From source file:com.ibm.crail.tools.CrailBenchmark.java
void readRandom(String filename, int size, int loop, boolean buffered) throws Exception { System.out.println("readRandom, filename " + filename + ", size " + size + ", loop " + loop + ", buffered " + buffered);// w w w . ja v a 2s .c om CrailBuffer buf = null; if (size == CrailConstants.BUFFER_SIZE) { buf = fs.allocateBuffer(); } else if (size < CrailConstants.BUFFER_SIZE) { CrailBuffer _buf = fs.allocateBuffer(); _buf.clear().limit(size); buf = _buf.slice(); } else { buf = OffHeapBuffer.wrap(ByteBuffer.allocateDirect(size)); } //warmup ConcurrentLinkedQueue<CrailBuffer> bufferQueue = new ConcurrentLinkedQueue<CrailBuffer>(); bufferQueue.add(buf); warmUp(filename, warmup, bufferQueue); //benchmark System.out.println("starting benchmark..."); fs.getStatistics().reset(); CrailFile file = fs.lookup(filename).get().asFile(); CrailBufferedInputStream bufferedStream = file.getBufferedInputStream(file.getCapacity()); CrailInputStream directStream = file.getDirectInputStream(file.getCapacity()); double sumbytes = 0; double ops = 0; long _range = file.getCapacity() - ((long) buf.capacity()); double range = (double) _range; Random random = new Random(); long start = System.currentTimeMillis(); while (ops < loop) { if (buffered) { buf.clear(); double _offset = range * random.nextDouble(); long offset = (long) _offset; directStream.seek(offset); double ret = (double) directStream.read(buf).get().getLen(); if (ret > 0) { sumbytes = sumbytes + ret; ops = ops + 1.0; } else { break; } } else { buf.clear(); double _offset = range * random.nextDouble(); long offset = (long) _offset; bufferedStream.seek(offset); double ret = (double) bufferedStream.read(buf.getByteBuffer()); if (ret > 0) { sumbytes = sumbytes + ret; ops = ops + 1.0; } else { break; } } } long end = System.currentTimeMillis(); double executionTime = ((double) (end - start)) / 1000.0; double throughput = 0.0; double latency = 0.0; double sumbits = sumbytes * 8.0; if (executionTime > 0) { throughput = sumbits / executionTime / 1000.0 / 1000.0; latency = 1000000.0 * executionTime / ops; } bufferedStream.close(); directStream.close(); System.out.println("execution time " + executionTime); System.out.println("ops " + ops); System.out.println("sumbytes " + sumbytes); System.out.println("throughput " + throughput); System.out.println("latency " + latency); fs.getStatistics().print("close"); }
From source file:de.tum.bgu.msm.syntheticPopulationGenerator.kagawa.SyntheticPopJP.java
public static int[] select(double[] probabilities, int[] id, int sumProb) { // select item based on probabilities (for zero-based float array) int[] results = new int[2]; Random rand = new Random(); double selPos = sumProb * rand.nextDouble(); double sum = 0; for (int i = 0; i < probabilities.length; i++) { sum += probabilities[i];/*from w w w . jav a 2 s .c o m*/ if (sum > selPos) { //return i; results[0] = id[i]; results[1] = i; return results; } } results[0] = id[probabilities.length - 1]; results[1] = probabilities.length - 1; return results; }
From source file:de.tum.bgu.msm.syntheticPopulationGenerator.kagawa.SyntheticPopJP.java
public static int[] select(double[] probabilities, int[] id, double sumProb) { // select item based on probabilities (for zero-based float array) int[] results = new int[2]; Random rand = new Random(); double selPos = sumProb * rand.nextDouble(); double sum = 0; for (int i = 0; i < probabilities.length; i++) { sum += probabilities[i];// w w w . j a v a2 s . c om if (sum > selPos) { //return i; results[0] = id[i]; results[1] = i; return results; } } results[0] = id[probabilities.length - 1]; results[1] = probabilities.length - 1; return results; }
From source file:de.tum.bgu.msm.syntheticPopulationGenerator.kagawa.SyntheticPopJP.java
public static int[] select(double[] probabilities, int length, int[] id, double sumProb) { //select item based on probabilities and return the name //probabilities and name have more items than the required (max number of required items is set on "length") int[] results = new int[2]; Random rand = new Random(); double selPos = sumProb * rand.nextDouble(); double sum = 0; for (int i = 0; i < probabilities.length; i++) { sum += probabilities[i];/*from w w w.j av a 2 s . co m*/ if (sum > selPos) { //return i; results[0] = id[i]; results[1] = i; return results; } } results[0] = id[probabilities.length - 1]; results[1] = probabilities.length - 1; return results; }
From source file:de.tum.bgu.msm.syntheticPopulationGenerator.kagawa.SyntheticPopJP.java
public static int[] select(double[] probabilities, int[] id) { // select item based on probabilities (for zero-based float array) double sumProb = 0; int[] results = new int[2]; for (double val : probabilities) sumProb += val; Random rand = new Random(); double selPos = sumProb * rand.nextDouble(); double sum = 0; for (int i = 0; i < probabilities.length; i++) { sum += probabilities[i];/*from ww w .j av a 2s .c om*/ if (sum > selPos) { //return i; results[0] = id[i]; results[1] = i; return results; } } results[0] = id[probabilities.length - 1]; results[1] = probabilities.length - 1; return results; }
From source file:de.tum.bgu.msm.syntheticPopulationGenerator.kagawa.SyntheticPopJP.java
public static int[] select(double[] probabilities, int length, int[] id) { //select item based on probabilities and return the name //probabilities and name have more items than the required (max number of required items is set on "length") double sumProb = 0; int[] results = new int[2]; for (int i = 0; i < length; i++) { sumProb += probabilities[i];/*from w ww . ja v a 2 s . co m*/ } Random rand = new Random(); double selPos = sumProb * rand.nextDouble(); double sum = 0; for (int i = 0; i < probabilities.length; i++) { sum += probabilities[i]; if (sum > selPos) { //return i; results[0] = id[i]; results[1] = i; return results; } } results[0] = id[probabilities.length - 1]; results[1] = probabilities.length - 1; return results; }
From source file:de.tum.bgu.msm.syntheticPopulationGenerator.kagawa.SyntheticPopJP.java
private static double translateIncome(int incomeClass, double[] incomeThresholds, GammaDistributionImpl q) throws MathException, MathException { //provide the income value for each person give the income class. //income follows a gamma distribution that was calibrated using the microdata. Income thresholds are calculated for the stiches double income; int finish = 0; double low = 0; double high = 1; if (incomeClass == 90) { income = 0; // kein Einkommen /* } else if (incomeClass == 50) { income = 0; // Selbstndige/r Landwirt/in in der Hauptttigkeit } else if (incomeClass == 99) { income = -1; //keine Angabe*/ } else {/*from w ww . j a v a 2 s . c o m*/ if (incomeClass == 1) { low = 0; high = incomeThresholds[0]; } else if (incomeClass == 50) { // Selbstndige/r Landwirt/in in der Hauptttigkeit low = 0; //give them a random income following the distribution high = 1; } else if (incomeClass == 99) { //keine Angabe low = 0; //give them a random income following the distribution high = 1; } else if (incomeClass == incomeThresholds.length + 1) { low = incomeThresholds[incomeThresholds.length - 1]; high = 1; } else { int i = 2; while (finish == 0) { if (incomeClass > i) { i++; } else { finish = 1; low = incomeThresholds[i - 2]; high = incomeThresholds[i - 1]; } } } Random rnd = new Random(); double cummulativeProb = rnd.nextDouble() * (high - low) + low; income = q.inverseCumulativeProbability(cummulativeProb); } return income; }
From source file:com.ibm.bi.dml.test.utils.TestUtils.java
/** * <p>//from w w w . j a v a 2s. c o m * Generates a test matrix with the specified parameters as a two * dimensional array. * </p> * <p> * Set seed to -1 to use the current time as seed. * </p> * * @param rows * number of rows * @param cols * number of columns * @param min * minimum value * @param max * maximum value * @param sparsity * sparsity * @param seed * seed * @return random matrix */ public static double[][] generateTestMatrix(int rows, int cols, double min, double max, double sparsity, long seed) { double[][] matrix = new double[rows][cols]; Random random; if (seed == -1) random = TestUtils.random; else random = new Random(seed); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (random.nextDouble() > sparsity) continue; matrix[i][j] = (random.nextDouble() * (max - min) + min); // System.out.print(matrix[i][j] + "(" + i + "," + j + ")"); } // System.out.println(); } return matrix; }
From source file:org.apache.hadoop.hbase.master.TestRegionStateOnMasterFailure.java
/** * Writes some data to the table.//from ww w. j a v a 2 s . c o m * @throws InterruptedException */ private void fillTable() throws IOException, InterruptedException { Random rand = new Random(19387129L); HTable table = testUtil.createTable(TABLE_NAME, FAMILIES); for (int iStoreFile = 0; iStoreFile < 4; ++iStoreFile) { for (int iRow = 0; iRow < 100; ++iRow) { final byte[] row = Bytes.toBytes("row" + iRow); Put put = new Put(row); Delete del = new Delete(row); for (int iCol = 0; iCol < 10; ++iCol) { final byte[] cf = rand.nextBoolean() ? CF1 : CF2; final long ts = rand.nextInt(); final byte[] qual = Bytes.toBytes("col" + iCol); if (rand.nextBoolean()) { final byte[] value = Bytes .toBytes("value_for_row_" + iRow + "_cf_" + Bytes.toStringBinary(cf) + "_col_" + iCol + "_ts_" + ts + "_random_" + rand.nextLong()); put.add(cf, qual, ts, value); } else if (rand.nextDouble() < 0.8) { del.deleteColumn(cf, qual, ts); } else { del.deleteColumns(cf, qual, ts); } } table.put(put); table.delete(del); table.flushCommits(); } } testUtil.waitUntilAllRegionsAssigned(1); }