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

public static String myUUID() {
    StringBuilder sb = new StringBuilder();
    int start = 48, end = 58;
    appendChar(sb, start, end);/*from  ww  w .j av  a2s.co m*/
    appendChar(sb, 65, 90);
    appendChar(sb, 97, 123);
    String charSet = sb.toString();
    StringBuilder sb1 = new StringBuilder();
    for (int i = 0; i < 24; i++) {
        int len = charSet.length();
        int pos = new Random().nextInt(len);
        sb1.append(charSet.charAt(pos));
    }
    return sb1.toString();
}

From source file:Main.java

public static <T> T pickRandomItem(List<T> items) {
    return items.get(new Random().nextInt(items.size()));
}

From source file:Gesture.java

public static void generateDraw(Gesture gesture) {
    final Random random = new Random();
    final int computerDraw = random.nextInt(3);
    System.out.println(computerDraw);

    if (computerDraw > gesture.getType()) {
        System.out.println("You lose.");
    } else if (computerDraw < gesture.getType()) {
        System.out.println("You win.");
    } else {/*  w  w  w .  ja  v a2 s . c  o m*/
        System.out.println("It is a tie.");

    }
}

From source file:Main.java

/**
 * Gets random string/*from  w  ww . j a  v  a2 s.  c  o m*/
 *
 * @param length String length
 * @return Random string
 */
public static String getRandomString(int length) {
    char[] chars = "ABCDEFGHIJKLMONPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
    StringBuilder builder = new StringBuilder();
    Random random = new Random();
    for (int i = 0; i < length; i++) {
        char c = chars[random.nextInt(chars.length)];
        builder.append(c);
    }
    return builder.toString();
}

From source file:Main.java

public static String getText(Document doc, String tagName, boolean flag) {

    NodeList nodeList = doc.getElementsByTagName(tagName);
    int leng = nodeList.getLength();
    if (leng <= 0)
        return "";

    Random rnd = new Random();
    int ran = rnd.nextInt(leng);

    return nodeList.item(ran).getTextContent();
}

From source file:Main.java

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

From source file:Main.java

private static float randomInRange(float min, float max) {

    float randomFloat = new Random().nextFloat();
    return randomFloat < 0.5 ? ((1 - randomFloat) * (max - min) + min) : (randomFloat * (max - min) + min);
}

From source file:Main.java

public static int randInt(int min, int max) {
    Random rand = new Random();
    return rand.nextInt((max - min) + 1) + min;
}

From source file:Main.java

public static String getRandom(char[] sourceChar, int length) {
    if (sourceChar == null || sourceChar.length == 0 || length < 0) {
        return null;
    }// ww  w  . j  a va  2s. c  om

    StringBuilder str = new StringBuilder(length);
    Random random = new Random();
    for (int i = 0; i < length; i++) {
        str.append(sourceChar[random.nextInt(sourceChar.length)]);
    }
    return str.toString();
}

From source file:Main.java

public static int getRandomNumber(int min, int max) {
    return new Random().nextInt(max) % (max - min + 1) + min;
}