Example usage for java.util Random nextInt

List of usage examples for java.util Random nextInt

Introduction

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

Prototype

public int nextInt(int bound) 

Source Link

Document

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

Usage

From source file:Main.java

public static void shuffle_array(int[] array) {
    int index, temp;
    Random random = new Random();
    for (int i = array.length - 1; i > 0; i--) {
        index = random.nextInt(i + 1);
        temp = array[index];//w w  w  . ja va 2  s  . c o m
        array[index] = array[i];
        array[i] = temp;
    }
}

From source file:Main.java

public static int[] shuffleArray(int[] ar) {
    Random rnd = new Random();
    for (int i = ar.length - 1; i > 0; i--) {
        int index = rnd.nextInt(i + 1);
        // Simple swap
        int a = ar[index];
        ar[index] = ar[i];//from www  . ja v a2 s  .  c  om
        ar[i] = a;
    }

    return ar;
}

From source file:Main.java

public static String randomString(int stringLength) {
    char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    StringBuilder randomStringBuilder = new StringBuilder();
    Random random = new Random();
    for (int i = 0; i < stringLength; i++) {
        char c = chars[random.nextInt(chars.length)];
        randomStringBuilder.append(c);/*from w  w  w .j av  a  2 s  .co  m*/
    }
    return randomStringBuilder.toString();
}

From source file:Main.java

public static String getNumberRadim(int n) {
    if (n < 1 || n > 10) {
        throw new IllegalArgumentException("cannot random " + n + " bit number");
    }//w w w. j  ava 2s .  c om
    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:com.snowplowanalytics.snowplow.tracker.core.Util.java

public static int getTransactionId() {
    Random r = new Random(); //NEED ID RANGE
    return r.nextInt(999999 - 100000 + 1) + 100000;
}

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   w w w .  j  av a2  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:com.cloudbase.CBBoundary.java

private static String generateBoundary() {
    // Boundary delimiters must not appear within the encapsulated material, 
    // and must be no longer than 70 characters, not counting the two
    // leading hyphens.
    Random rand = new Random();
    final int count = rand.nextInt(11) + 30; // a random size from 30 to 40
    StringBuilder buffer = new StringBuilder(count);
    for (int i = 0; i < count; i++) {
        buffer.append(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]);
    }//www  .  ja  va2s  .  c om
    return buffer.toString();
}

From source file:Main.java

public static String generateRandomStrs(int strCount) {
    StringBuilder randomStrs = new StringBuilder();
    Random randomGenerator = new Random();
    for (int i = 1; i < strCount; i++) {
        int randomInt = randomGenerator.nextInt(26);
        randomStrs.append((char) (randomInt + 'a'));
    }// w  w  w  .j  a  v  a  2s  . c  o  m

    return randomStrs.toString();
}

From source file:Main.java

public static int getRandomNumber(int start, int end) {
    end++;/* w  w  w  .j a  v  a2s.  c  om*/
    Random random = new Random();
    int result = -1;

    if (start == 0) {
        result = random.nextInt(end);
    } else {
        result = random.nextInt(end);
        if (result < start) {
            result = result + start;
        }
    }
    return result;
}

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;//  www .j  a  va 2 s.c om
    }

    return result;
}