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 String randomStr(long strLen) {

    char freshChar;
    String freshString;/*from ww  w  .java2 s .  com*/
    freshString = "";

    while (freshString.length() < (strLen - 1)) {

        freshChar = (char) (Math.random() * 128);
        if (Character.isLetter(freshChar)) {
            freshString += freshChar;
        }
    }

    return (freshString);

}

From source file:Main.java

public static int getRandomColor() {
    int size = sColorArray.size();
    int index = (int) (Math.random() * size);
    return sColorArray.get(index);
}

From source file:Main.java

public static long getRandomLong4() {
    Random rd = new Random();
    long l2 = 0;//from  ww w . j  a  v a  2 s  .  c om
    for (int i = 1; i < 100; i++) {
        long l1 = (int) (Math.random() * 9000 + 1000);
        l2 = rd.nextInt(9000) + 1000;
    }
    return l2;
}

From source file:Main.java

public static void invokeMethodFormRed5(String toUserId) {
    Date nowDate = new Date();
    String time = nowDate.getTime() + "" + (int) ((Math.random() * 100) % 100);
    message = time;//from www . j  a  v  a  2 s.c  o m
    //      connection.call("createMeeting", responder, User.id + "", toUserId, message);
    Log.d("DEBUG", "call createMeeting");
}

From source file:Main.java

public static String getRandomNumString(int length) {
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < length; i++) {
        builder.append(String.valueOf((int) (Math.random() * 10)));
    }//from  www.ja  v a 2  s  .c o m
    return builder.toString();
}

From source file:Main.java

public static Set<Integer> randomSetRange(int min, int max, int n) {
    Set<Integer> set = new HashSet<Integer>();
    while (set.size() < n) {
        int num = (int) (Math.random() * (max - min)) + min;
        set.add(num);/* w w w.  j  a va  2s .c  o  m*/
    }
    return set;

}

From source file:Main.java

/**
 * Pick a random index from a list of items
 * @param items - the collection of data
 * @return a index value between 0 and n-1
 *///www.  j  a  va2s.co  m
@SafeVarargs
public static <T> int randomIndex(T... items) {
    int picked = (int) (Math.random() * items.length);
    return picked;
}

From source file:Main.java

private static String justifyOperation(String s, float width, Paint p) {
    float holder = (float) (COMPLEXITY * Math.random());
    while (s.contains(Float.toString(holder))) {
        holder = (float) (COMPLEXITY * Math.random());
    }/*  w  ww .j  a v  a  2  s  .  co  m*/
    String holder_string = Float.toString(holder);
    float lessThan = width;
    int timeOut = 100;
    int current = 0;
    while ((p.measureText(s) < lessThan) && (current < timeOut)) {
        s = s.replaceFirst(" ([^" + holder_string + "])", " " + holder_string + "$1");
        lessThan = (p.measureText(holder_string) + lessThan) - p.measureText(" ");
        current++;
    }
    String cleaned = s.replaceAll(holder_string, " ");
    return cleaned;
}

From source file:Main.java

private static int getRandomNum() {
    return (int) (Math.random() * 0xFF);
}

From source file:Main.java

public static String getRandom(int num) {
    StringBuffer sbf = new StringBuffer();
    for (int i = 0; i < num; i++) {
        int random = (int) (Math.random() * (RANDOMS.length()));
        sbf.append(RANDOMS.charAt(random));
    }/*from  w w  w.  j  a  v a  2  s  . c om*/
    return sbf.toString();

}