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 String generateRandomString(int len) {
    String str = "0123456789abcdefghijklmnopqrstuvwxyz";
    Random rnd = new Random();
    StringBuilder sb = new StringBuilder(len);
    for (int i = 0; i < len; i++) {
        sb.append(str.charAt(rnd.nextInt(str.length())));
    }/*w w w.  j a v  a 2 s .  c o  m*/
    return sb.toString();
}

From source file:Main.java

public static String getRandomKey(int length) {
    String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    Random random = new Random();
    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < length; ++i) {
        int number = random.nextInt(str.length() - 1);//[0,62)

        sb.append(str.charAt(number));/*w w  w .j a v a  2s .  c  o  m*/
    }
    return sb.toString();
}

From source file:Main.java

public static void genRandom(long seed, int max, int factor, int offset, int array[]) {
    Random r = new Random(seed);
    for (int i = 0; i < array.length; i++) {
        array[i] = (r.nextInt(max) * factor + offset);
    }//from  w  ww .  jav  a  2  s  .co m
}

From source file:Main.java

public static String generateNickname() {
    Random rnd = new Random(System.currentTimeMillis());
    int race_selection = rnd.nextInt(3);

    String thefirstname = "";
    String thelastname = "";
    String thefirstname1 = "";
    String thelastname1 = "";

    if (race_selection == 0) {
        double fnprefix1 = Math.floor(Math.random() * 122);
        double fnsuffix1 = Math.floor(Math.random() * 91);
        double lnprefix1 = Math.floor(Math.random() * 67);
        double lnsuffix1 = Math.floor(Math.random() * 64);

        thefirstname = h_fnpre.get((int) fnprefix1) + h_fnsuf.get((int) fnsuffix1);
        thelastname = h_lnpre.get((int) lnprefix1) + h_lnsuf.get((int) lnsuffix1);

        thefirstname1 = thefirstname.substring(0, 1).toUpperCase();
        thefirstname = thefirstname1 + thefirstname.substring(1, thefirstname.length());

        thelastname1 = thelastname.substring(0, 1).toUpperCase();
        thelastname = thelastname1 + thelastname.substring(1, thelastname.length());

    } else if (race_selection == 0) {
        double fnprefix1 = Math.floor(Math.random() * 80);
        double fnsuffix1 = Math.floor(Math.random() * 67);

        thefirstname = o_fnpre.get((int) fnprefix1) + o_fnsuf.get((int) fnsuffix1);
        thelastname = "";

    } else {/*from w w  w . ja v a  2 s .  c om*/
        double fnprefix1 = Math.floor(Math.random() * 122);
        double fnsuffix1 = Math.floor(Math.random() * 91);

        thefirstname = h_fnpre.get((int) fnprefix1) + h_fnsuf.get((int) fnsuffix1);

    }

    return thefirstname + " " + thelastname;
}

From source file:Main.java

public static String generateRandom() {
    Random number = new Random();
    StringBuilder message = new StringBuilder();
    for (int i = 0; i < 1405; i++) {
        message.append('a' + number.nextInt(52));
    }// w  w w. j  a v a 2 s  . c o m
    return message.toString();
}

From source file:net.dovemq.transport.link.LinkTestUtils.java

public static CAMQPMessage createMessage(Random randomGenerator) {
    String deliveryTag = UUID.randomUUID().toString();
    int sectionSize = 256 * (randomGenerator.nextInt(10) + 1);
    String str = RandomStringUtils.randomAlphanumeric(sectionSize);
    CAMQPMessagePayload payload = new CAMQPMessagePayload(str.getBytes());
    return new CAMQPMessage(deliveryTag, payload);
}

From source file:Main.java

public static <T> java.util.List<T> randomSample(java.util.List<T> items, int size) {
    size = Math.min(size, items.size());
    java.util.Random rnd = new java.util.Random();
    for (int i = 0; i < size; i++) {
        int pos = i + rnd.nextInt(items.size() - i);
        T tmp = items.get(pos);//from  www .  j a  v  a2  s .c  o m
        items.set(pos, items.get(i));
        items.set(i, tmp);
    }
    return items.subList(0, size);
}

From source file:Main.java

public static int randInt(int min, int max) {

    // Usually this should be a field rather than a method variable so
    // that it is not re-seeded every call.
    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:Test.java

private static void startUpdatingThread(final List<String> list) {
    updatingThread = new Thread(new Runnable() {
        long counter = 0;

        public void run() {
            while (!Thread.interrupted()) {
                int size = list.size();
                Random random = new Random();
                if (random.nextBoolean()) {
                    if (size > 1) {
                        list.remove(random.nextInt(size - 1));
                    }// ww  w .  j  a v  a  2 s.c o m
                } else {
                    if (size < 20) {
                        list.add("Random string " + counter);
                    }
                }
                counter++;
            }
        }

    });
    updatingThread.start();
}

From source file:Main.java

public static String RandomString(int length) {
    String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    Random rnd = new Random();

    StringBuilder sb = new StringBuilder(length);
    for (int i = 0; i < length; i++) {
        sb.append(AB.charAt(rnd.nextInt(AB.length())));
    }//from  w  w w.java  2s. com
    return sb.toString();

}