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 double cauchy(double u, double s) {
    double res = Math.random();
    double x = Math.tan(Math.PI * (res - 0.5));
    return u + s * x;
}

From source file:Main.java

public static <V> V getRandom(Collection<V> collection) {

    if (collection.isEmpty()) {
        return null;
    }/*from  ww w  . j a  v a 2s . c  om*/

    int randIdx = (int) Math.round(Math.random() * (collection.size() - 1));
    int count = 0;
    Iterator<V> iterator = collection.iterator();
    while (iterator.hasNext()) {
        V current = iterator.next();
        if (count == randIdx) {
            return current;
        }
        count++;
    }

    throw new RuntimeException("Shouldn't happen");
}

From source file:Main.java

public static String toNumberPassword(int length) {
    if (length < 1) {
        return null;
    }//  w  ww  . j  av a  2 s . c o m
    String strChars[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
    StringBuffer strPassword = new StringBuffer();
    int nRand = (int) Math.round(Math.random() * 100D);
    for (int i = 0; i < length; i++) {
        nRand = (int) Math.round(Math.random() * 100D);
        strPassword.append(strChars[nRand % (strChars.length - 1)]);
    }
    return strPassword.toString();
}

From source file:com.cssweb.android.connect.ConnPool.java

public static JSONObject sendReq(String funcname, String funcno, String reqbuf) throws JSONException {
    StringBuffer sb = new StringBuffer();
    sb.append(TradeUtil.getGlobalRequest(funcname, funcno));
    sb.append(reqbuf);/*ww w  . ja v  a2 s  .  c  o m*/
    sb.append("&ram=" + Math.random());
    String sbStr = sb.toString();
    if ("190101".equals(funcno) || "203113".equals(funcno) || "203111".equals(funcno) || "203526".equals(funcno)
            || "203119".equals(funcno) || "202010".equals(funcno) || "202012".equals(funcno)) {
        sbStr = sbStr.replace("isSafe=0", "isSafe=1");
    }
    Log.i("==", sbStr);
    String req = "";
    try {
        req = new String(Base64.encode(sbStr.getBytes("gb2312")));
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    }
    req = TradeUtil.getResult(req);

    Log.i(">>>>>url>>>>>>>", getURL() + req);
    return Conn.tradeReq(getURL() + req);
}

From source file:Main.java

/**
 * Gat random item./*from  w  w  w. j a  va  2 s  .co  m*/
 *
 * @param items
 *            the items
 * @return the object
 */
// //////////////////////////////////////////////////////////
public static Object gatRandomItem(final List items) {
    if (items == null || items.size() == 0) {
        return null;
    }
    final int itemIndex = (int) (Math.random() * items.size());
    return items.get(itemIndex);
}

From source file:Main.java

public static double frandom(double a, double b)
// Random real within range [a,b[
{
    return (b - a) * Math.random() + a;
}

From source file:Main.java

public static int irandom(int a, int b)
// Random integer within range {a,...,b}    
{
    return (int) ((b + 1 - a) * Math.random()) + a;
}

From source file:Main.java

/**
 * Method to add a random delay or if none to yield to give any waiting
 * threads a chance. This helps make test threaded code more random to track
 * synchronized issues.//w  ww.j  a  v  a2 s.  co m
 *
 * @param millis
 *            up to the many ms
 * @return actual sleep time
 */
public static final int sleepUpTo(final long millis) {
    try {
        if (millis > 0) {
            int realSleep = (int) Math.floor(Math.random() * (millis + 1));
            if (realSleep > 0) {
                Thread.sleep(realSleep);
                return realSleep;
            } else {
                // Give any waiting threads a go
                Thread.yield();
                return 0;
            }
        }
    } catch (final InterruptedException e) {
        // It's not an error that we were Interrupted!! Don't log the
        // exception !
        return -1;
    }
    return 0;
}

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

private static XYDataset createDataset() {
    DefaultXYDataset defaultxydataset = new DefaultXYDataset();
    double ad[] = new double[1000];
    double ad1[] = new double[1000];
    for (int i = 0; i < 1000; i++) {
        ad[i] = Math.random() + 1.0D;
        ad1[i] = Math.random() + 1.0D;
    }/*  w  w  w  .jav a 2s  .c  o  m*/

    double ad2[][] = { ad, ad1 };
    defaultxydataset.addSeries("Series 1", ad2);
    return defaultxydataset;
}

From source file:net.estinet.gFeatures.Feature.FusionPlay.GameUtil.WorldUtil.java

public static void initializeGame() {
    FusionGame fg = FusionPlay.currentGame;
    int random = (int) (Math.random() * (fg.getMaps().size()));
    Debug.print("[FusionPlay] World magic number: " + random);
    FusionPlay.currentGame.setFusionMap(fg.getMaps().get(random));
    copyWorld(fg.getMaps().get(random).getFile());
}