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:com.betfair.application.performance.BaselinePerformanceTester.java

private static HttpCallable getRequest(Random rnd) {
    return requests.getValue(rnd.nextInt(requests.getMaxRange()));
}

From source file:com.infovity.iep.loader.util.SupplierLoaderUtil.java

public static int gen() {
    Random r = new Random(System.currentTimeMillis());
    return ((1 + r.nextInt(2)) * 10000 + r.nextInt(10000));
}

From source file:com.betfair.application.performance.BaselinePerformanceTester.java

private static String getContentType(Random rnd) {
    return protocols.getValue(rnd.nextInt(protocols.getMaxRange()));
}

From source file:fastcall.FArrayUtils.java

/**
 * Shuffle an int array//ww  w .  j  a  va  2 s  . c  o  m
 * Implementing FisherYates shuffle
 * @param ar 
 */
public static void 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];
        ar[i] = a;
    }
}

From source file:fastcall.FArrayUtils.java

/**
 * Shuffle an double array/* w w  w  .j  ava2s.co  m*/
 * Implementing FisherYates shuffle
 * @param ar 
 */
public static void shuffleArray(double[] ar) {
    Random rnd = new Random();
    for (int i = ar.length - 1; i > 0; i--) {
        int index = rnd.nextInt(i + 1);
        // Simple swap
        double a = ar[index];
        ar[index] = ar[i];
        ar[i] = a;
    }
}

From source file:com.liferay.portal.security.pwd.PwdEncryptor.java

private static byte[] _getSaltFromCrypt(String cryptString) throws PwdEncryptorException {

    byte[] saltBytes = null;

    try {//w w  w  .ja  v  a 2 s  . c o  m
        //if (Validator.isNull(cryptString)) {
        if (StringUtils.isEmpty(cryptString)) {

            // Generate random salt

            Random random = new Random();

            int numSaltChars = saltChars.length;

            StringBuilder sb = new StringBuilder();

            int x = random.nextInt(Integer.MAX_VALUE) % numSaltChars;
            int y = random.nextInt(Integer.MAX_VALUE) % numSaltChars;

            sb.append(saltChars[x]);
            sb.append(saltChars[y]);

            String salt = sb.toString();

            //saltBytes = salt.getBytes(Digester.ENCODING);
            saltBytes = salt.getBytes("UTF8");
        } else {

            // Extract salt from encrypted password

            String salt = cryptString.substring(0, 2);

            //saltBytes = salt.getBytes(Digester.ENCODING);
            saltBytes = salt.getBytes("UTF8");
        }
    } catch (UnsupportedEncodingException uee) {
        throw new PwdEncryptorException("Unable to extract salt from encrypted password: " + uee.getMessage());
    }

    return saltBytes;
}

From source file:net.sf.javaml.tools.DatasetTools.java

/**
 * Generate a bootstrap sample from the data set with a particular size,
 * using the given random generator./*ww w.  j a v a 2s .  co m*/
 * 
 * This is done by sampling with replacement.
 * 
 * @param data
 *            data set to bootstrap from
 * @param size
 *            number of instances in the output data
 * @param rg
 *            random generator to use for the bootstrapping
 * @return bootstrap of the supplied data
 */
@Deprecated
public static Dataset bootstrap(Dataset data, int size, Random rg) {
    Dataset out = new DefaultDataset();
    while (out.size() < size) {
        out.add(data.instance(rg.nextInt(data.size())).copy());
    }
    return out;
}

From source file:com.feilong.commons.core.util.StringUtilTest.java

/**
 * ?150?.//from  w  ww. j  a  va 2  s  .c o m
 * 
 * @return the random string
 */
private static String getRandomString() {
    StringBuilder sb = new StringBuilder();
    Random r = new Random();
    int length = 150 + r.nextInt(50);
    for (int i = 0; i < length; i++) {
        sb.append('a' + r.nextInt(26));
    }
    return sb.toString();
}

From source file:com.espertech.esper.epl.variable.VariableServiceCallable.java

private static int[] getIndexesShuffled(int length, Random random, int loopNum) {
    int[] indexRandomized = new int[length];

    for (int i = 0; i < indexRandomized.length; i++) {
        indexRandomized[i] = i;// w  ww .  ja  v  a  2s.c  o  m
    }

    for (int i = 0; i < length; i++) {
        int indexOne = random.nextInt(length);
        int indexTwo = random.nextInt(length);
        int temp = indexRandomized[indexOne];
        indexRandomized[indexOne] = indexRandomized[indexTwo];
        indexRandomized[indexTwo] = temp;
    }

    return indexRandomized;
}

From source file:Main.java

/**
 * Fills the array with random longs.  If signed is true, negative values can be generated.
 * The values will fit within 'numberOfBits'.  This is useful for conversion tests.
 *///from w  w  w  .  j a v  a 2  s  .  com
public static void genRandomLongs(long seed, long array[], boolean signed, int numberOfBits) {
    long positiveMask = numberOfBits == 64 ? -1 : ((1l << numberOfBits) - 1);
    long negativeMask = ~positiveMask;
    Random r = new Random(seed);
    for (int i = 0; i < array.length; i++) {
        long l = r.nextLong();
        if (signed && l < 0) {
            l = l | negativeMask;
        } else {
            l = l & positiveMask;
        }
        array[i] = l;
    }
    // Seed a few special numbers we want to be sure to test.
    array[r.nextInt(array.length)] = 0l;
    array[r.nextInt(array.length)] = 1l;
    array[r.nextInt(array.length)] = positiveMask;
    if (signed) {
        array[r.nextInt(array.length)] = negativeMask;
        array[r.nextInt(array.length)] = -1;
    }
}