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 getRandomMixString(int length) {
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < length; i++) {
        int randomInt = (int) (Math.random() * 10);
        if ((randomInt % 2) == 0) {
            char c = (char) (randomInt + 97);
            builder.append(c);//from   w  w w  .j  av a2s .co m
            continue;
        }
        builder.append(String.valueOf(randomInt));
    }

    return builder.toString();

}

From source file:alluxio.cli.JournalCrashTest.java

/**
 * Runs the crash test.//  w ww  .j a v a2  s  . c o m
 *
 * @param args no arguments
 */
public static void main(String[] args) {
    // Parse the input args.
    if (!parseInputArgs(args)) {
        System.exit(EXIT_FAILED);
    }

    System.out.println("Stop the current Alluxio cluster...");
    stopCluster();

    // Set NO_STORE and NO_PERSIST so that this test can work without AlluxioWorker.
    sCreateFileOptions = CreateFileOptions.defaults().setWriteType(WriteType.NONE);
    // Set the max retry to avoid long pending for client disconnect.
    if (System.getProperty(PropertyKey.USER_RPC_RETRY_MAX_NUM_RETRY.toString()) == null) {
        System.setProperty(PropertyKey.USER_RPC_RETRY_MAX_NUM_RETRY.toString(), "10");
    }

    System.out.println("Start Journal Crash Test...");
    long startTimeMs = System.currentTimeMillis();
    boolean ret = true;
    startMaster();

    int rounds = 0;
    while (System.currentTimeMillis() - startTimeMs < sTotalTimeMs) {
        rounds++;
        long aliveTimeMs = (long) (Math.random() * sMaxAliveTimeMs) + 100;
        LOG.info("Round {}: Planning Master Alive Time {}ms.", rounds, aliveTimeMs);

        System.out.println("Round " + rounds + " : Launch Clients...");
        sFileSystem = FileSystem.Factory.get();
        try {
            sFileSystem.delete(new AlluxioURI(sTestDir));
        } catch (Exception e) {
            // Test Directory not exist
        }

        // Launch all the client threads.
        setupClientThreads();
        for (Thread thread : sClientThreadList) {
            thread.start();
        }

        CommonUtils.sleepMs(LOG, aliveTimeMs);
        System.out.println("Round " + rounds + " : Crash Master...");
        killMaster();
        for (ClientThread clientThread : sClientThreadList) {
            clientThread.setIsStopped(true);
        }
        for (Thread thread : sClientThreadList) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                LOG.error("Error when waiting thread", e);
            }
        }

        System.out.println("Round " + rounds + " : Check Status...");
        startMaster();
        boolean checkSuccess = false;
        try {
            checkSuccess = checkStatus();
        } catch (Exception e) {
            LOG.error("Failed to check status", e);
        }
        CliUtils.printPassInfo(checkSuccess);
        ret &= checkSuccess;
    }

    stopCluster();
    System.exit(ret ? EXIT_SUCCESS : EXIT_FAILED);
}

From source file:Main.java

public static String randomAlphaNumeric(int length) {
    StringBuffer buffer = new StringBuffer();
    String characters = "abcdefghijklmnopqrstuvwxyz0123456789";
    int charactersLength = characters.length();
    for (int i = 0; i < length; i++) {
        double index = Math.random() * charactersLength;
        buffer.append(characters.charAt((int) index));
    }//from  ww  w  . j  ava  2  s  .c o m
    return buffer.toString();
}

From source file:Main.java

public static int GetRandomIndex(int min, int max) {
    return (int) (Math.random() * (max - min + 1)) + min;
}

From source file:Main.java

/**
 * chooses a random color from array.xml
 *//*from w  ww . j  a  v  a2  s  .  c  om*/
public static int getRandomMaterialColor(Context context, String typeColor) {
    int returnColor = Color.GRAY;
    int arrayId = context.getResources().getIdentifier("mdcolor_" + typeColor, "array",
            context.getPackageName());

    if (arrayId != 0) {
        TypedArray colors = context.getResources().obtainTypedArray(arrayId);
        int index = (int) (Math.random() * colors.length());
        returnColor = colors.getColor(index, Color.GRAY);
        colors.recycle();
    }
    return returnColor;
}

From source file:Main.java

/**
 * Get a random number within a given range;
 *//*from  www  .  j  a  v a2s.co m*/
public static int getRandomInt(int min, int max) throws Exception {

    int ret;
    int range = max - min;

    ret = (int) (Math.random() * min) + range;

    Log.i(TAG, String.format("Generate an integer value between %d and %d: %d", min, max, ret));

    return ret;
}

From source file:alluxio.examples.JournalCrashTest.java

/**
 * Runs the crash test.//  w  w w  .  ja v a  2 s  .  co m
 *
 * Usage:
 * {@code java -cp
 * alluxio-<ALLUXIO-VERSION>-jar-with-dependencies.jar alluxio.examples.JournalCrashTest}
 *
 * @param args no arguments
 */
public static void main(String[] args) {
    // Parse the input args.
    if (!parseInputArgs(args)) {
        System.exit(EXIT_FAILED);
    }

    System.out.println("Stop the current Alluxio cluster...");
    stopCluster();

    // Set NO_STORE and NO_PERSIST so that this test can work without AlluxioWorker.
    sCreateFileOptions = CreateFileOptions.defaults().setWriteType(WriteType.NONE);
    // Set the max retry to avoid long pending for client disconnect.
    if (System.getProperty(Constants.MASTER_RETRY_COUNT) == null) {
        System.setProperty(Constants.MASTER_RETRY_COUNT, "10");
    }

    System.out.println("Start Journal Crash Test...");
    long startTimeMs = System.currentTimeMillis();
    boolean ret = true;
    startMaster();

    int rounds = 0;
    while (System.currentTimeMillis() - startTimeMs < sTotalTimeMs) {
        rounds++;
        long aliveTimeMs = (long) (Math.random() * sMaxAliveTimeMs) + 100;
        LOG.info("Round {}: Planning Master Alive Time {}ms.", rounds, aliveTimeMs);

        System.out.println("Round " + rounds + " : Launch Clients...");
        sFileSystem = FileSystem.Factory.get();
        try {
            sFileSystem.delete(new AlluxioURI(sTestDir));
        } catch (Exception e) {
            // Test Directory not exist
        }

        // Launch all the client threads.
        setupClientThreads();
        for (Thread thread : sClientThreadList) {
            thread.start();
        }

        CommonUtils.sleepMs(LOG, aliveTimeMs);
        System.out.println("Round " + rounds + " : Crash Master...");
        killMaster();
        for (ClientThread clientThread : sClientThreadList) {
            clientThread.setIsStopped(true);
        }
        for (Thread thread : sClientThreadList) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                LOG.error("Error when waiting thread", e);
            }
        }

        System.out.println("Round " + rounds + " : Check Status...");
        startMaster();
        boolean checkSuccess = false;
        try {
            checkSuccess = checkStatus();
        } catch (Exception e) {
            LOG.error("Failed to check status", e);
        }
        Utils.printPassInfo(checkSuccess);
        ret &= checkSuccess;
    }

    stopCluster();
    System.exit(ret ? EXIT_SUCCESS : EXIT_FAILED);
}

From source file:Main.java

public static float rnd(final float min, final float max) {
    float fRandNum = (float) Math.random();
    return min + (max - min) * fRandNum;
}

From source file:Main.java

public static void getTop(double[] array, ArrayList<Integer> rankList, int i) {
    int index = 0;
    HashSet<Integer> scanned = new HashSet<Integer>();
    double max = Double.MIN_VALUE;
    for (int m = 0; m < i && m < array.length; m++) {
        max = Double.MIN_VALUE;// w  ww  .j  ava2  s  . c  o m
        for (int no = 0; no < array.length; no++) {
            if (!scanned.contains(no)) {
                if (array[no] > max) {
                    index = no;
                    max = array[no];
                } else if (array[no] == max && Math.random() > 0.5) {
                    index = no;
                    max = array[no];
                }
            }
        }
        if (!scanned.contains(index)) {
            scanned.add(index);
            rankList.add(index);
        }
        //System.out.println(m + "\t" + index);
    }
}

From source file:Main.java

public static <T> T random(List<T> list) {
    return list.get((int) Math.floor(Math.random() * list.size()));
}