Example usage for java.util Random nextDouble

List of usage examples for java.util Random nextDouble

Introduction

In this page you can find the example usage for java.util Random nextDouble.

Prototype

public double nextDouble() 

Source Link

Document

Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

Usage

From source file:at.illecker.hama.hybrid.examples.summation.SummationBSP.java

static BigDecimal writeSummationInputFile(FileSystem fs, Path dir, int fileCount) throws IOException {

    BigDecimal sum = new BigDecimal(0);
    Random rand = new Random();
    double rangeMin = 0;
    double rangeMax = 100;

    for (int i = 0; i < fileCount; i++) {
        DataOutputStream out = fs.create(new Path(dir, "part" + i));

        // loop between 50 and 149 times
        for (int j = 0; j < rand.nextInt(100) + 50; j++) {
            // generate key value pair inputs
            double randomValue = rangeMin + (rangeMax - rangeMin) * rand.nextDouble();

            String truncatedValue = new BigDecimal(randomValue)
                    .setScale(DOUBLE_PRECISION, BigDecimal.ROUND_DOWN).toString();

            String line = "key" + (j + 1) + "\t" + truncatedValue + "\n";
            out.writeBytes(line);/*from  w  ww . j  a  va 2  s .  c  om*/

            sum = sum.add(new BigDecimal(truncatedValue));
            LOG.debug("input[" + j + "]: '" + line + "' sum: " + sum.toString());
        }
        out.close();
    }
    return sum;
}

From source file:com.linkedin.pinot.core.segment.index.creator.RawIndexCreatorTest.java

/**
 * Helper method that generates a random value for a given data type
 *
 * @param dataType Data type for which to generate the random value
 * @return Random value for the data type.
 *///  www .  j av a  2s.  c  o  m
public static Object getRandomValue(Random random, FieldSpec.DataType dataType) {
    Object value;
    switch (dataType) {
    case INT:
        value = random.nextInt();
        break;

    case LONG:
        value = random.nextLong();
        break;

    case FLOAT:
        value = random.nextFloat();
        break;

    case DOUBLE:
        value = random.nextDouble();
        break;

    case STRING:
        value = StringUtil
                .trimTrailingNulls(RandomStringUtils.random(random.nextInt(MAX_STRING_LENGTH_IN_BYTES)));
        break;

    default:
        throw new IllegalArgumentException("Illegal data type for random value generator: " + dataType);
    }
    return value;
}

From source file:org.apache.hadoop.hbase.rest.TestScannerResource.java

static int insertData(Configuration conf, String tableName, String column, double prob) throws IOException {
    Random rng = new Random();
    int count = 0;
    HTable table = new HTable(conf, tableName);
    byte[] k = new byte[3];
    byte[][] famAndQf = KeyValue.parseColumn(Bytes.toBytes(column));
    for (byte b1 = 'a'; b1 < 'z'; b1++) {
        for (byte b2 = 'a'; b2 < 'z'; b2++) {
            for (byte b3 = 'a'; b3 < 'z'; b3++) {
                if (rng.nextDouble() < prob) {
                    k[0] = b1;//from  w  ww  .  jav  a 2 s . c om
                    k[1] = b2;
                    k[2] = b3;
                    Put put = new Put(k);
                    put.setDurability(Durability.SKIP_WAL);
                    put.add(famAndQf[0], famAndQf[1], k);
                    table.put(put);
                    count++;
                }
            }
        }
    }
    table.flushCommits();
    table.close();
    return count;
}

From source file:it.units.malelab.ege.util.Utils.java

public static <T> T selectRandom(Map<T, Double> options, Random random) {
    double sum = 0;
    for (Double rate : options.values()) {
        sum = sum + rate;/*from w  w  w .java2 s . c  o  m*/
    }
    double d = random.nextDouble() * sum;
    for (Map.Entry<T, Double> option : options.entrySet()) {
        if (d < option.getValue()) {
            return option.getKey();
        }
        d = d - option.getValue();
    }
    return (T) options.keySet().toArray()[0];
}

From source file:org.apache.hama.pipes.TestPipes.java

static double[][] createRandomMatrix(int rows, int columns, Random rand) {
    final double[][] matrix = new double[rows][columns];
    double rangeMin = 0;
    double rangeMax = 100;

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            double randomValue = rangeMin + (rangeMax - rangeMin) * rand.nextDouble();
            matrix[i][j] = new BigDecimal(randomValue).setScale(DOUBLE_PRECISION, BigDecimal.ROUND_DOWN)
                    .doubleValue();/*from  w  ww. ja v  a 2s. c  o m*/
        }
    }
    return matrix;
}

From source file:org.apache.sysml.utils.lite.BuildLiteExecution.java

public static double[][] randomMatrix(int rows, int cols, double min, double max, double sparsity) {
    double[][] matrix = new double[rows][cols];
    Random random = new Random(System.currentTimeMillis());
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (random.nextDouble() > sparsity) {
                continue;
            }/*from w w  w .  j  a v  a 2  s.c  o m*/
            matrix[i][j] = (random.nextDouble() * (max - min) + min);
        }
    }
    return matrix;
}

From source file:lirmm.inria.fr.math.BigSparseRealMatrix.java

/**
 * Generate a matrix of random values./*w  w w.ja v  a 2  s .  c om*/
 *
 * @param rowDimension Number of rows of the matrix.
 * @param columnDimension Number of columns of the matrix.
 * @return An instance of BigSparseRealMatrix.
 */
public static BigSparseRealMatrix randomGenerateMatrix(int rowDimension, int columnDimension) {
    BigSparseRealMatrix m = new BigSparseRealMatrix(rowDimension, columnDimension);
    Random r = new Random();
    for (int i = 0; i < rowDimension; i++) {
        for (int j = 0; j < columnDimension; j++) {
            m.setEntry(i, j, r.nextDouble());
        }
    }
    return m;
}

From source file:org.apache.hama.pipes.TestPipes.java

static BigDecimal writeSummationInputFile(FileSystem fs, Path dir) throws IOException {
    DataOutputStream out = fs.create(new Path(dir, "part0"));
    Random rand = new Random();
    double rangeMin = 0;
    double rangeMax = 100;
    BigDecimal sum = new BigDecimal(0);
    // loop between 50 and 149 times
    for (int i = 0; i < rand.nextInt(100) + 50; i++) {
        // generate key value pair inputs
        double randomValue = rangeMin + (rangeMax - rangeMin) * rand.nextDouble();
        String truncatedValue = new BigDecimal(randomValue).setScale(DOUBLE_PRECISION, BigDecimal.ROUND_DOWN)
                .toString();/*from   w  w  w. ja  v  a  2 s .c om*/

        String line = "key" + (i + 1) + "\t" + truncatedValue + "\n";
        out.writeBytes(line);

        sum = sum.add(new BigDecimal(truncatedValue));
    }
    out.close();
    return sum;
}

From source file:org.deeplearning4j.datasets.canova.RecordReaderDataSetiteratorTest.java

public static double[][] makeRandomCSV(String tempFile, int nLines, int nFeatures) {
    File temp = new File(tempFile);
    temp.deleteOnExit();//from   w  w  w .  ja v  a  2  s  .c om
    Random rand = new Random(12345);

    double[][] dArr = new double[nLines][nFeatures + 1];

    try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(temp)))) {
        for (int i = 0; i < nLines; i++) {
            dArr[i][0] = rand.nextDouble(); //First column: label
            out.print(dArr[i][0]);
            for (int j = 0; j < nFeatures; j++) {
                dArr[i][j + 1] = rand.nextDouble();
                out.print("," + dArr[i][j + 1]);
            }
            out.println();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return dArr;
}

From source file:com.twitter.graphjet.bipartite.MultiSegmentPowerLawBipartiteGraphTest.java

/**
 * Build a random left-regular bipartite graph of given left and right sizes.
 *
 * @param leftSize   is the left hand size of the bipartite graph
 * @param rightSize  is the right hand size of the bipartite graph
 * @param random     is the random number generator to use for constructing the graph
 * @return a random bipartite graph//from  w  w w.  ja v  a  2s . co  m
 */
public static MultiSegmentPowerLawBipartiteGraph buildRandomMultiSegmentBipartiteGraph(int maxNumSegments,
        int maxNumEdgesPerSegment, int leftSize, int rightSize, double edgeProbability, Random random) {
    MultiSegmentPowerLawBipartiteGraph multiSegmentPowerLawBipartiteGraph = new MultiSegmentPowerLawBipartiteGraph(
            maxNumSegments, maxNumEdgesPerSegment, leftSize / 2, (int) (rightSize * edgeProbability / 2), 2.0,
            rightSize / 2, (int) (leftSize * edgeProbability / 2), 2.0, new IdentityEdgeTypeMask(),
            new NullStatsReceiver());
    for (int i = 0; i < leftSize; i++) {
        for (int j = 0; j < rightSize; j++) {
            if (random.nextDouble() < edgeProbability) {
                multiSegmentPowerLawBipartiteGraph.addEdge(i, j, (byte) 0);
            }
        }
    }

    return multiSegmentPowerLawBipartiteGraph;
}