Example usage for java.lang Math random

List of usage examples for java.lang Math random

Introduction

In this page you can find the example usage for java.lang Math random.

Prototype

public static double random() 

Source Link

Document

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0 .

Usage

From source file:org.zilverline.util.Utils.java

/**
 * Picks a random number between 0 and (below) the givenNumber.
 * //from ww  w  . jav  a 2s.  c om
 * @param number the givenNumber
 * 
 * @return a number n: 0 <= n < givenNumber
 */
public static int pickOne(final int number) {
    return (int) Math.round(Math.random() * (double) (number - 1));
}

From source file:Main.java

/**
 * Gets a list of integers(size specified by the given size) between the
 * specified start(inclusion) and end(inclusion) randomly.
 *
 * @param start the specified start//from  ww w.  jav a  2 s .  c  o m
 * @param end the specified end
 * @param size the given size
 * @return a list of integers
 */
public static List<Integer> getRandomIntegers(final int start, final int end, final int size) {
    if (size > (end - start + 1)) {
        throw new IllegalArgumentException("The specified size more then (end - start + 1)!");
    }

    final List<Integer> integers = genIntegers(start, end);
    final List<Integer> ret = new ArrayList<Integer>();

    int remainsSize;
    int index;

    while (ret.size() < size) {
        remainsSize = integers.size();
        index = (int) (Math.random() * (remainsSize - 1));
        final Integer i = integers.get(index);

        ret.add(i);
        integers.remove(i);
    }

    return ret;
}

From source file:utils.RandomVariable.java

/**
 * Generate a random number between 0 and 1.
 *
 * @return a double between 0 and 1.//from w w  w.j a va  2s.  com
 */
public static double rand() {
    double x = Math.random();
    return x;
}

From source file:com.offbynull.peernetic.debug.visualizer.VisualizerUtils.java

/**
 * Creates a random point within a rectangle.
 * @param width rectangle width//from  ww w .ja  v a 2 s.com
 * @param height rectangle height
 * @return new random point within rectangle specified by {@code width} and {@code height}
 * @throws IllegalArgumentException if any argument is negative or a special double value (e.g. NaN/infinite/etc..)
 */
public static Point randomPointInRectangle(double width, double height) {
    Validate.inclusiveBetween(0.0, Double.MAX_VALUE, width);
    Validate.inclusiveBetween(0.0, Double.MAX_VALUE, height);
    return new Point((int) (Math.random() * width), (int) (Math.random() * height));
}

From source file:gov.nih.nci.caintegrator.application.zip.FileNameGenerator.java

public static String generateFileNameFromEmail(String userEmail) throws ValidationException {
    String fileName = null;/*w w  w .  ja v a  2 s.c o m*/
    if (userEmail != null && EmailValidator.getInstance().isValid(userEmail)) {
        // Get a random number of up to 7 digits      
        int sevenDigitRandom = new Double(Math.random() * 10000000).intValue();

        // Get the last 7 digits of the Java timestamp
        String s = String.valueOf(System.currentTimeMillis());
        String lastSevenOfTimeStamp = s.substring(s.length() - 7, s.length());

        // Get the string before the @ sign in the user's email address
        String userEmailBeforeAtSign = userEmail.substring(0, userEmail.indexOf('@'));

        // Put it all together
        fileName = userEmailBeforeAtSign + "_" + sevenDigitRandom + lastSevenOfTimeStamp;
    } else {
        throw new ValidationException("Invalid Email Address");
    }
    return fileName;
}

From source file:org.jfree.chart.demo.Animator.java

@SuppressWarnings("rawtypes")
public void actionPerformed(ActionEvent actionevent) {
    int i = (int) (Math.random() * (double) dataset.getRowCount());
    Comparable comparable = dataset.getRowKey(i);
    int j = (int) (Math.random() * (double) dataset.getColumnCount());
    Comparable comparable1 = dataset.getColumnKey(j);
    int k = Math.random() - 0.5D >= 0.0D ? 5 : -5;
    dataset.setValue(Math.max(0.0D, dataset.getValue(i, j).doubleValue() + (double) k), comparable,
            comparable1);/*from  w w w.java2s  . c  o  m*/
}

From source file:ttma.client.GUI.StatFinance.java

private static CategoryDataset createDataset() {
    final String expenses = "EXPENSES";
    final String incomes = "INCOMES";

    final String hotels = "Hotels";
    final String events = "Events";
    final String pub = "Publicity";
    final String trans = "Transoport";
    final String sal = "Salaries";
    final String cl = "Clinics";
    final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    double he = 100 + (int) (Math.random() * ((8000 - 100) + 1));
    double ee = 100 + (int) (Math.random() * ((8000 - 100) + 1));
    double pe = 100 + (int) (Math.random() * ((8000 - 100) + 1));
    double te = 100 + (int) (Math.random() * ((8000 - 100) + 1));
    double se = 100 + (int) (Math.random() * ((8000 - 100) + 1));
    double ce = 100 + (int) (Math.random() * ((8000 - 100) + 1));

    int hi = 100 + (int) (Math.random() * ((8000 - 100) + 1));
    int ei = 100 + (int) (Math.random() * ((8000 - 100) + 1));

    int ti = 100 + (int) (Math.random() * ((8000 - 100) + 1));

    int ci = 100 + (int) (Math.random() * ((8000 - 100) + 1));

    dataset.addValue(he, expenses, hotels);
    dataset.addValue(ee, expenses, events);
    dataset.addValue(pe, expenses, pub);
    dataset.addValue(te, expenses, trans);
    dataset.addValue(se, expenses, sal);
    dataset.addValue(ce, expenses, cl);/*from w  w  w  .  j  ava2 s  . c  om*/

    dataset.addValue(hi, incomes, hotels);
    dataset.addValue(ei, incomes, events);
    dataset.addValue(0.1, incomes, pub);
    dataset.addValue(ti, incomes, trans);
    dataset.addValue(0.1, incomes, sal);
    dataset.addValue(ci, incomes, cl);

    return dataset;
}

From source file:pl.altkom.sping.batch.sample.processor.CustomerItemProcessor.java

@Override
public Customer process(Customer customer) throws Exception {

    System.out.println("customer " + customer);

    customer.setMargin(Math.random());

    System.out.println("margin " + customer.getMargin());

    return customer;

}

From source file:TimerBasedAnimation.java

public TimerBasedAnimation() {
    setXY(20 * Math.random(), 200, 200);

    timer = new Timer(20, this);
    timer.setInitialDelay(190);
    timer.start();
}

From source file:com.eu.evaluation.server.eva.Thread1.java

public void run() {
    try {//from   w  ww.  j ava 2  s .c o  m
        double t = Math.random();
        long sleep = (long) (t * 10000);
        Thread.sleep(sleep);
        logger.debug(" : " + threadName + "  " + sleep);
    } catch (InterruptedException ex) {
        Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
    }
}