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:Main.java

public static int getRandomNum(int maxNum) {
    return (int) (Math.random() * maxNum);
}

From source file:Main.java

public static String randomString(int len, String charset) {
    String out = "";

    for (int i = 0; i < len; i++) {
        out += charset.charAt((int) (Math.random() * charset.length()));
    }//from w ww. j  a  v a  2 s . c  o  m

    return out;
}

From source file:Main.java

public static List<String> populateStringList(int numberOfItems) {
    final String partialWord = "partialListParallel";
    List<String> dummyStringList = null;
    if (numberOfItems > 0) {
        dummyStringList = new ArrayList<String>();
        for (int i = 0; i < numberOfItems; i++) {
            final double randomNumber = (Math.random() * numberOfItems);
            final StringBuilder sb = new StringBuilder(partialWord).append(randomNumber);
            dummyStringList.add(sb.toString());
        }//from  w w w  .  j a  va  2  s . c o m
    }
    return dummyStringList;
}

From source file:Main.java

public static String getRandomString(int length) {
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < length; i++) {
        int number = random.nextInt(3);
        long result = 0;
        switch (number) {
        case 0:/*  w  w  w. java 2s  .co  m*/
            result = Math.round(Math.random() * 25 + 65);
            sb.append(String.valueOf((char) result));
            break;
        case 1:
            result = Math.round(Math.random() * 25 + 97);
            sb.append(String.valueOf((char) result));
            break;
        case 2:
            sb.append(String.valueOf(new Random().nextInt(10)));
            break;
        }

    }
    return sb.toString();
}

From source file:Main.java

public static Employee employeeMaker() {
    List<String> names = Arrays.asList("John Doe", "Jane Doe", "Mark Twain", "Oliver Twist", "Ferdinand IV");

    return new Employee(names.get((int) (Math.random() * names.size())), Math.random() * 20_000);
}

From source file:Main.java

private static String getSessionComponent() {
    return Integer.toHexString((int) ((Math.random() + 1) * 0x10000)).substring(1);
}

From source file:Main.java

public static Uri getRandomRingtone(Context context) {
    Uri alert = null;/*w  ww  .ja v a  2 s.c  om*/
    RingtoneManager ringtoneManager = new RingtoneManager(context);
    ringtoneManager.setType(RingtoneManager.TYPE_ALARM);
    int count = ringtoneManager.getCursor().getCount();
    int attempts = 0;
    do {
        int random = (int) Math.random() * (count + 1);
        alert = ringtoneManager.getRingtoneUri(random);
    } while (alert == null && ++attempts < 5);
    return alert;
}

From source file:Main.java

public static int randomInt(int upto) {

    int number;//w  w  w . j a v  a  2 s. co  m

    number = (int) Math.floor(Math.random() * upto);

    return number;
}

From source file:Main.java

public static Color getRandColor(int threashold) {
    if (threashold < 0 || threashold > 255)
        throw new IllegalArgumentException("Threashold is not between 0 and 255");
    int secondOperand = 255 - threashold;
    return new Color((int) (Math.random() * secondOperand) + threashold,
            (int) (Math.random() * secondOperand) + threashold,
            (int) (Math.random() * secondOperand) + threashold);
}

From source file:Main.java

/** Returns a random value in the range [1 - range] */
public static long random(long range) {
    return (long) ((Math.random() * range) % range) + 1;
}