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:edu.eci.arsw.model.Board.java

public Board() {
    Calendar cal = Calendar.getInstance();
    ID = dateFormat.format(cal.getTime());
    Random rnd = new Random();
    Integer[] used = new Integer[width * height];
    int val, i, j;
    for (i = 0; i < width * height; i++)
        used[i] = 0;/*from w w  w.j av a 2 s.c o m*/
    for (i = 0; i < width; i++) {
        for (j = 0; j < height; j++) {
            val = (int) (rnd.nextDouble() * width * height);
            while (used[val] != 0)
                val = (int) (rnd.nextDouble() * width * height);
            used[val] = 1;
            board[i][j] = val;
        }
    }
    for (i = 0; i < width * height; i++)
        used[i] = 0;
    for (i = 0; i < cards2Find; i++) {
        val = (int) (rnd.nextDouble() * width * height);
        while (used[val] != 0)
            val = (int) (rnd.nextDouble() * width * height);
        used[val] = 1;
        cards[i] = val;
    }
}

From source file:pltag.parser.params.ProbVec.java

public int sample(Random random) {
    final double target = random.nextDouble() * sum;
    int i = -1;/*w w  w . j ava 2 s. c  o m*/
    double accum = 0.0;
    while (accum < target) {
        i += 1;
        accum += counts[i];
    }
    return i;
}

From source file:bachelorthesis.methods.detection.bayesian.BayesianDetection.java

/**
 * Generates time series for code testing
 * @param numberOfParts number of parts with constant mean
 * @param samePointsMin   min number of points per part
 * @param samePointsMax max number of points per part
 * @param maxDeviation maximal deviation of mean (e.g. 0.1)
 * @return generated time series with given specs
 *//*from w  ww.  j ava2  s.com*/
private Value[] generate(int numberOfParts, int samePointsMin, int samePointsMax, double maxDeviation) {
    Value[] data = new Value[0];
    int sum = EARLIEST_YEAR;
    for (int i = 0; i < numberOfParts; i++) {
        Random rand = new Random();
        int numberOfPoints = rand.nextInt(samePointsMax - samePointsMin + 1) + samePointsMin;
        double mean = rand.nextDouble();
        double var = rand.nextDouble() * maxDeviation;
        System.out.println(sum + " - " + (sum + numberOfPoints) + "\t" + mean);
        sum += numberOfPoints;
        Value[] add = new Value[numberOfPoints];
        for (int j = 0; j < numberOfPoints; j++) {
            add[j] = new Value(mean + ((rand.nextDouble() * 2 - 1) * var));
        }
        data = ArrayUtils.addAll(data, add);
    }
    return data;
}

From source file:pltag.parser.params.SparseVec.java

public int sample(Random random) {
    final double target = random.nextDouble() * sum;
    int i = -1;//from  w  w w.j  av a  2 s.  c o  m
    double accum = 0.0;
    while (accum < target) {
        i += 1;
        accum += counts.getEntry(i);
    }
    return i;
}

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

private Map<Long, Double> generate() {
    Map<Long, Double> map = new HashMap<>();
    Random r = new Random();
    for (int i = 0; i < 2000; ++i) {
        map.put(r.nextLong(), r.nextDouble());
    }//from   www . j  a v a 2 s  .  c  om
    return map;
}

From source file:org.canova.api.util.MathUtils.java

/**
 * This will generate a series of uniformally distributed
 * numbers between l times//from  w  ww . ja  v  a2  s.c o m
 *
 * @param l the number of numbers to generate
 * @return l uniformally generated numbers
 */
public static double[] generateUniform(int l) {
    double[] ret = new double[l];
    java.util.Random rgen = new java.util.Random();
    for (int i = 0; i < l; i++) {
        ret[i] = rgen.nextDouble();
    }
    return ret;
}

From source file:edu.jhuapl.graphs.jfreechart.JFreeChartTimeSeriesGraphSourceTestCase.java

private double getNextRandomValue(Random r) {
    // we have a 90% chance of something in 0-10, and a 10% chance of something in 10-20
    if (r.nextDouble() <= .9) {
        return 10 * r.nextDouble();
    } else {/* w ww  .j  a  v a 2  s . co  m*/
        return 10 + 10 * r.nextDouble();
    }
}

From source file:org.orekit.utils.FieldPVCoordinatesTest.java

private PolynomialFunction randomPolynomial(int degree, Random random) {
    double[] coeff = new double[1 + degree];
    for (int j = 0; j < degree; ++j) {
        coeff[j] = random.nextDouble();
    }//from  w  w w.j  ava2s .  c  o  m
    return new PolynomialFunction(coeff);
}

From source file:org.pentaho.di.trans.steps.cpythonscriptexecutor.CPythonScriptExecutorData.java

/**
 * Generate some random rows to send to python in the case where a single variable (data frame) is being extracted
 * and we want to try and determine the types of the output fields
 *
 * @param inputMeta incoming row meta/*from ww  w. ja  v a 2  s  .c o m*/
 * @param r         Random instance to use
 * @return a list of randomly generated rows with types matching the incoming row types.
 * @throws KettleException if a problem occurs
 */
protected static List<Object[]> generateRandomRows(RowMetaInterface inputMeta, Random r)
        throws KettleException {
    List<Object[]> rows = new ArrayList<Object[]>(NUM_RANDOM_ROWS);
    // ValueMetaInterface numericVM = new ValueMeta( "num", ValueMetaInterface.TYPE_NUMBER ); //$NON-NLS-1$
    ValueMetaInterface numericVM = ValueMetaFactory.createValueMeta("num", ValueMetaInterface.TYPE_NUMBER); //$NON-NLS-1$

    for (int i = 0; i < NUM_RANDOM_ROWS; i++) {
        Object[] currentRow = new Object[inputMeta.size()];
        for (int j = 0; j < inputMeta.size(); j++) {
            ValueMetaInterface vm = inputMeta.getValueMeta(j);

            ValueMetaInterface tempVM = vm.clone();
            tempVM.setStorageType(ValueMetaInterface.STORAGE_TYPE_NORMAL);

            Object newVal;
            double d = r.nextDouble();
            switch (vm.getType()) {
            case ValueMetaInterface.TYPE_NUMBER:
            case ValueMetaInterface.TYPE_INTEGER:
            case ValueMetaInterface.TYPE_BIGNUMBER:
                d *= 100.0;
                newVal = d;
                if (vm.getStorageType() == ValueMetaInterface.STORAGE_TYPE_BINARY_STRING) {
                    newVal = tempVM.convertData(numericVM, newVal);
                }
                currentRow[j] = vm.getStorageType() == ValueMetaInterface.STORAGE_TYPE_NORMAL
                        ? vm.convertData(numericVM, newVal)
                        : tempVM.convertToBinaryStringStorageType(newVal);
                break;
            case ValueMetaInterface.TYPE_DATE:
                newVal = new Date(new Date().getTime() + (long) (d * 100000));
                currentRow[j] = vm.getStorageType() == ValueMetaInterface.STORAGE_TYPE_NORMAL ? newVal
                        : tempVM.convertToBinaryStringStorageType(newVal);
                break;
            case ValueMetaInterface.TYPE_TIMESTAMP:
                newVal = new Timestamp(new Date().getTime() + (long) (d * 100000));
                currentRow[j] = vm.getStorageType() == ValueMetaInterface.STORAGE_TYPE_NORMAL ? newVal
                        : tempVM.convertToBinaryStringStorageType(newVal);
                break;
            case ValueMetaInterface.TYPE_BOOLEAN:
                newVal = r.nextBoolean();
                currentRow[j] = vm.getStorageType() == ValueMetaInterface.STORAGE_TYPE_NORMAL ? newVal
                        : tempVM.convertToBinaryStringStorageType(newVal);
                break;
            default:
                newVal = d < 0.5 ? "value1" : "value2";
                currentRow[j] = vm.getStorageType() == ValueMetaInterface.STORAGE_TYPE_NORMAL ? newVal
                        : tempVM.convertToBinaryStringStorageType(newVal);
            }
        }
        rows.add(currentRow);
    }
    return rows;
}

From source file:com.linkedin.thirdeye.hadoop.derivedcolumn.transformation.DerivedColumnNoTransformationTest.java

private long generateRandomHoursSinceEpoch() {
    Random r = new Random();
    // setting base value to year 2012
    long unixtime = (long) (1293861599 + r.nextDouble() * 60 * 60 * 24 * 365);
    return TimeUnit.SECONDS.toHours(unixtime);
}