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 getNumberRadim(int n) {
    if (n < 1 || n > 10) {
        throw new IllegalArgumentException("cannot random " + n + " bit number");
    }//www  .j a  v a  2s  . c o m
    Random ran = new Random();
    if (n == 1) {
        return String.valueOf(ran.nextInt(10));
    }
    int bitField = 0;
    char[] chs = new char[n];
    for (int i = 0; i < n; i++) {
        while (true) {
            int k = ran.nextInt(10);
            if ((bitField & (1 << k)) == 0) {
                bitField |= 1 << k;
                chs[i] = (char) (k + '0');
                break;
            }
        }
    }
    return new String(chs);
}

From source file:Main.java

public static String get_random_motivation_message() {
    int idx = new Random().nextInt(mm.length);
    return (mm[idx]);
}

From source file:Main.java

public static ArrayList<Integer> shuffleArray(int size) {
    ArrayList<Integer> shuffled = new ArrayList<>();
    for (int i = 0; i < size; i++) {
        shuffled.add(i);/*from  ww w .j  a v a 2  s  . c om*/
    }

    Random rnd = new Random();
    for (int i = size - 1; i > 0; i--) {
        int index = rnd.nextInt(i + 1);
        // Simple swap
        int a = shuffled.get(index);
        shuffled.set(index, shuffled.get(i));
        shuffled.set(i, a);
    }

    return shuffled;
}

From source file:Main.java

public static String generateFileName(String fileName) {
    DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
    String formatDate = format.format(new Date());
    int random = new Random().nextInt(10000);
    int position = fileName.lastIndexOf(".");
    String extension = fileName.substring(position);

    return formatDate + random + extension;
}

From source file:Main.java

public static double getRandomNumber(double start, double end) {
    double random = new Random().nextDouble();
    double result = start + (random * (end - start));
    return result;
}

From source file:Main.java

public static int nextInt(int lower, int upper) {
    Random ra = new Random();
    return (lower + ra.nextInt(upper - lower + 1));
}

From source file:Main.java

/**
 * Get a random number within a given upper limit;
 *//*from w w  w  . ja  va2s .c  o m*/
public static int getRandomInt(int max) throws Exception {
    int ret;
    Random r = new Random();
    ret = r.nextInt(max);

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

    return ret;
}

From source file:Main.java

/**
 * Returns a valid IP v4 address.//from ww  w .j a va 2s . c om
 *
 * @return [0-255].[0-255].[0-255].[0-255]
 */
public static String generateRandomIp() {
    try {
        Random r = new Random();
        String ip = r.nextInt(256) + "." + r.nextInt(256) + "." + r.nextInt(256) + "." + r.nextInt(256);
        if (isValidIp(ip)) {
            return ip;
        } else {
            return generateRandomIp();
        }
    } catch (Exception e) {
    }
    return "127.0.0.1";
}

From source file:Main.java

public static String getRandomString(int length) {
    if (length <= 0)
        return null;

    StringBuffer sb = new StringBuffer();
    Random r = new Random();
    int range = RANDOM_CHARS.length();
    for (int i = 0; i < length; i++) {
        sb.append(RANDOM_CHARS.charAt(r.nextInt(range)));
    }/*from w w  w.  j a v a 2 s .com*/
    return sb.toString();
}

From source file:Main.java

static String generateHoodieId() {
    int hoodieIDLength = 7;
    String alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";
    int alphabetLength = alphabet.length();

    String result = "";

    Random rnd = new Random();

    for (int i = 0; i < hoodieIDLength; i++) {
        int r = rnd.nextInt(alphabetLength);
        char c = alphabet.charAt(r);

        result += c;/*from w  ww  . j av  a  2  s  .co  m*/
    }

    return result;
}