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:net.duckling.ddl.util.StringUtil.java

License:asdf

public static String getRandomString(int length) {
    StringBuffer buffer = new StringBuffer("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM");
    StringBuffer sb = new StringBuffer();
    Random r = new Random();
    int range = buffer.length();
    for (int i = 0; i < length; i++) {
        sb.append(buffer.charAt(r.nextInt(range)));
    }//from  w  w w . j  a va  2 s  .  co  m
    return sb.toString();
}

From source file:com.linkedin.paldb.impl.GenerateTestData.java

public static int[][] generateIntArrayData(int count, int size) {
    int[][] res = new int[count][];
    Random random = new Random(count + 34593263544354353l);
    for (int i = 0; i < count; i++) {
        int[] r = new int[size];
        for (int j = 0; j < size; j++) {
            r[j] = random.nextInt(1000000);
        }/* w w  w  .j  a v  a2s  .  com*/
        res[i] = r;
    }
    return res;
}

From source file:com.cherong.mock.common.base.util.EncryptionUtil.java

public static String generateSalt(int size) {
    StringBuffer buffer = new StringBuffer();
    Random random = new Random();
    int temp = 0;
    for (int i = 0; i < size; i++) {
        temp = random.nextInt(hexDigits.length);
        buffer.append(hexDigits[temp]);//from w  w w  .  j a v  a2s.c  o m
    }
    return buffer.toString();
}

From source file:Main.java

public static void genRandom(long seed, int max, int factor, int offset, int array[], int stride, int skip) {
    Random r = new Random(seed);
    for (int i = 0; i < array.length / stride; i++) {
        for (int j = 0; j < stride; j++) {
            if (j >= stride - skip)
                array[i * stride + j] = 0;
            else/*from  w ww  .ja v a 2s . c  o m*/
                array[i * stride + j] = r.nextInt(max) * factor + offset;
        }
    }
}

From source file:com.microsoft.windowsazure.mobileservices.zumoe2etestapp.framework.Util.java

public static String createComplexRandomString(Random rndGen, int size) {
    if (rndGen.nextInt(3) > 0) {
        return createSimpleRandomString(rndGen, size);
    } else {//from www.j ava2 s.  co  m
        return createSimpleRandomString(rndGen, size, ' ', 0xfffe);
    }
}

From source file:com.inclouds.hbase.utils.RegionServerPoker.java

private static ServerName getServerName(NavigableMap<HRegionInfo, ServerName> map, int seed) {
    Collection<ServerName> servers = map.values();
    List<ServerName> list = new ArrayList<ServerName>();
    for (ServerName sn : servers) {
        list.add(sn);/* w w w .  jav a2 s.  com*/
    }

    Collections.sort(list, new Comparator<ServerName>() {
        @Override
        public int compare(ServerName arg0, ServerName arg1) {
            return arg0.getHostname().compareTo(arg1.getHostname());
        }

    });

    Random r = new Random(seed);
    int index = r.nextInt(list.size());

    return list.get(index);
}

From source file:com.tinypace.mobistore.util.StringUtil.java

public static String RandomNumbString(int len) {
    final int maxNum = 10;
    int i; // ??   
    int count = 0; // ??   
    char[] str = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

    StringBuffer pwd = new StringBuffer("");
    Random r = new Random();
    while (count < len) {
        // ?????//from   www  .ja  v a2  s  .co  m
        i = Math.abs(r.nextInt(maxNum)); // ?36-1   
        if (i >= 0 && i < str.length) {
            pwd.append(str[i]);
            count++;
        }
    }
    return pwd.toString();
}

From source file:me.xhh.utils.Util.java

/**
 * @param len//from  w  w w  .j a v  a 2 s  .c  o  m
 *            length of code to be generated
 * @param humanReadable
 *            whether to generate a human-readable code (excluding some confusing characters like 1 and l)
 * @return a random code, containing lower-case letters and numbers
 */
public static String generateCode(int len, boolean humanReadable) {
    final char[] codeArr = humanReadable
            ? new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'r', 's', 't', 'u',
                    'v', 'w', 'x', 'y', 'z', '2', '3', '4', '5', '6', '7', '8', '9' }
            : new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
                    'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8',
                    '9' };
    Random rand = new Random();
    StringBuilder code = new StringBuilder();

    for (int i = 0; i < len; i++)
        code.append(codeArr[rand.nextInt(codeArr.length)]);

    return code.toString();
}

From source file:de.unentscheidbar.csv2.CaseInsensitiveBenchmark.java

static Collection<String> getLotsOfColumnNames() {

    Random rnd = new Random(0);
    Set<String> names = Collections.newSetFromMap(new CaseInsensitiveMap<String, Boolean>());
    while (names.size() < 100) {
        names.add(randomString(rnd, 1 + rnd.nextInt(30)));
    }/* w w  w  . j a  v a  2s  . c  o  m*/
    List<String> uniqueNames = new ArrayList<>(names);
    /* For predictable iteration order: */
    Collections.sort(uniqueNames);
    Collections.shuffle(uniqueNames, rnd);
    return Collections.unmodifiableList(uniqueNames);
}

From source file:com.tinypace.mobistore.util.StringUtil.java

/**
 * /*w  w w. j  ava2s .  co m*/
 *???
 * @param len 
 * @return 
 */
public static String RandomString(int len) {
    final int maxNum = 10;
    int i; // ??   
    int count = 0; // ??   
    char[] str = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
            's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

    //        char[] str = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };   

    StringBuffer pwd = new StringBuffer("");
    Random r = new Random();
    while (count < len) {
        // ?????
        i = Math.abs(r.nextInt(maxNum)); // ?36-1   
        if (i >= 0 && i < str.length) {
            pwd.append(str[i]);
            count++;
        }
    }
    return pwd.toString();
}