Example usage for java.util Random Random

List of usage examples for java.util Random Random

Introduction

In this page you can find the example usage for java.util Random Random.

Prototype

public Random() 

Source Link

Document

Creates a new random number generator.

Usage

From source file:Main.java

/**
 * Returns a pseudo-random number between min and max, inclusive.
 * The difference between min and max can be at most
 * <code>Integer.MAX_VALUE - 1</code>.
 *
 * @param min Minimum value/*from www .  ja  va2 s  . c  o m*/
 * @param max Maximum value.  Must be greater than min.
 * @return Integer between min and max, inclusive.
 * @see java.util.Random#nextInt(int)
 */
public static int randInt(int min, int max) {

    // Usually this can be a field rather than a method variable
    Random rand = new Random();

    // nextInt is normally exclusive of the top value,
    // so add 1 to make it inclusive
    int randomNum = rand.nextInt((max - min) + 1) + min;

    return randomNum;
}

From source file:Main.java

protected static String generateRandomString(int demoLength) {
    String base = "abcdefghijklmnopqrstuvwxyz";
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < demoLength; i++) {
        int number = random.nextInt(base.length());
        sb.append(base.charAt(number));/*ww w  .j  a  va  2s  .  c o m*/
    }
    return sb.toString();
}

From source file:Coin.java

public String toss() {
    Random myRand = new Random();
    int face = myRand.nextInt(2);
    if (face == 0) {
        return "heads";
    } else {//from  www  . jav  a  2 s .  c o  m
        return "tails";
    }
}

From source file:Test.java

private static void startUpdateThread(int i, final ConcurrentMap<Integer, String> concurrentMap) {
    Thread thread = new Thread(new Runnable() {
        public void run() {
            while (!Thread.interrupted()) {
                Random random = new Random();
                int randomInt = random.nextInt(20);
                concurrentMap.put(randomInt, UUID.randomUUID().toString());
            }//from w  w  w. ja  v  a  2  s . c om
        }
    });
    thread.setName("Update Thread " + i);
    updateThreads.add(thread);
    thread.start();
}

From source file:Main.java

public static String getRandomFileName(String filePath) {
    SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss", Locale.getDefault());
    Date date = new Date();
    String key = format.format(date);

    Random r = new Random();
    key = key + r.nextInt();//  w w w  .  j  av a 2s.c  om
    key = key.substring(0, 15);
    return filePath + "/" + key + ".jpeg";
}

From source file:Main.java

/**
 * Shuffle an array./*from w  w w.  j  a  va  2  s  . c om*/
 * @param array Array.
 */
private static void Shuffle(float[] array) {
    Random random = new Random();
    for (int i = array.length - 1; i > 0; i--) {
        int index = random.nextInt(i + 1);
        float temp = array[index];
        array[index] = array[i];
        array[i] = temp;
    }
}

From source file:Main.java

public static boolean[] generateRandomBooleanArray(int size) {
    boolean[] res = new boolean[size];
    Random r = new Random();
    for (int i = 0; i < size; i++)
        res[i] = r.nextBoolean();//  w w w . j  a  va2 s. c om
    return res;
}

From source file:com.dhenton9000.selenium.generic.UtilMethods.java

public static String generateRandomName(String rootName) {
    Random r = new Random();
    String randomStr = Long.toString(Math.abs(r.nextLong()), 21);
    String randomName = rootName + "_" + randomStr.substring(0, 4);
    return randomName;
}

From source file:Main.java

public static final String randomString(int length) {
    Random randGen = null;/* w  w w  .j ava  2 s. co  m*/
    char[] numbersAndLetters = null;
    Object initLock = new Object();
    if (length < 1) {
        return null;
    }
    if (randGen == null) {
        synchronized (initLock) {
            if (randGen == null) {
                randGen = new Random();
                numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz"
                        + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
            }
        }
    }
    char[] randBuffer = new char[length];
    for (int i = 0; i < randBuffer.length; i++) {
        randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
    }
    return new String(randBuffer);
}

From source file:Main.java

protected static byte[] getBogusNodeID() {
    byte[] id = new byte[20];

    new Random().nextBytes(id);

    return (id);//from  www .j a v a 2  s  . c o m
}