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.microsoft.windowsazure.mobileservices.zumoe2etestapp.framework.Util.java

public static String createSimpleRandomString(Random rndGen, int size, int minChar, int maxChar) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < size; i++) {

        int charRand;
        char c;//w  ww .j av  a 2  s.com
        do {
            charRand = rndGen.nextInt(maxChar - minChar);
            c = (char) (minChar + charRand);
        } while (Character.isLowSurrogate(c) || Character.isHighSurrogate(c));

        sb.append(c);
    }

    return sb.toString();
}

From source file:Main.java

/**
 * Fills the array with random doubles.  Values will be between min (inclusive) and
 * max (inclusive)./*from w  w  w.j a v  a 2  s .  c  o m*/
 */
public static void genRandomDoubles(long seed, double min, double max, double array[],
        boolean includeExtremes) {
    Random r = new Random(seed);
    int minExponent = Math.min(Math.getExponent(min), 0);
    int maxExponent = Math.max(Math.getExponent(max), 0);
    if (minExponent < -6 || maxExponent > 6) {
        // Use an exponential distribution
        int exponentDiff = maxExponent - minExponent;
        for (int i = 0; i < array.length; i++) {
            double mantissa = r.nextDouble();
            int exponent = minExponent + r.nextInt(maxExponent - minExponent);
            int sign = (min >= 0) ? 1 : 1 - r.nextInt(2) * 2; // -1 or 1
            double rand = sign * mantissa * Math.pow(2.0, exponent);
            if (rand < min || rand > max) {
                continue;
            }
            array[i] = rand;
        }
    } else {
        // Use a linear distribution
        for (int i = 0; i < array.length; i++) {
            double rand = r.nextDouble();
            array[i] = min + rand * (max - min);
        }
    }
    // Seed a few special numbers we want to be sure to test.
    for (int i = 0; i < sInterestingDoubles.length; i++) {
        double d = sInterestingDoubles[i];
        if (min <= d && d <= max) {
            array[r.nextInt(array.length)] = d;
        }
    }
    array[r.nextInt(array.length)] = min;
    array[r.nextInt(array.length)] = max;
    if (includeExtremes) {
        array[r.nextInt(array.length)] = Double.NaN;
        array[r.nextInt(array.length)] = Double.POSITIVE_INFINITY;
        array[r.nextInt(array.length)] = Double.NEGATIVE_INFINITY;
        array[r.nextInt(array.length)] = Double.MIN_VALUE;
        array[r.nextInt(array.length)] = Double.MIN_NORMAL;
        array[r.nextInt(array.length)] = Double.MAX_VALUE;
        array[r.nextInt(array.length)] = -Double.MIN_VALUE;
        array[r.nextInt(array.length)] = -Double.MIN_NORMAL;
        array[r.nextInt(array.length)] = -Double.MAX_VALUE;
    }
}

From source file:dremel.common.AvroTest.java

private static Utf8 generateRandomUtf8(Random rand, int maxLength) {
    Utf8 utf8 = new Utf8().setLength(rand.nextInt(maxLength));
    for (int i = 0; i < utf8.getLength(); i++) {
        utf8.getBytes()[i] = (byte) ('a' + rand.nextInt('z' - 'a'));
    }//from w  w w  .j  av a  2  s.  co m
    return utf8;
}

From source file:org.hdiv.util.HDIVUtil.java

/**
 * Generates a random number between 0 (inclusive) and n (exclusive).
 * //  w  w  w  .  j  a  va  2s . c  om
 * @param n
 *            the bound on the random number to be returned. Must be positive.
 * @return Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and <code>n</code>
 *         (exclusive).
 * @since HDIV 1.1
 */
public static String createRandomToken(int n) {

    Random r = new Random();
    int i = r.nextInt(n);
    if (i == 0) {
        i = 1;
    }

    return String.valueOf(i);
}

From source file:Main.java

/**
 * This method generates a random n digit password, which contains at least one number, lower case alphabet, upper
 * case alphabet and as special character.
 *///from   ww w .  jav a 2s .  c  om
public static String generatePassword(int n) {

    Random rd = new Random();

    char lowerChars[] = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    char upperChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    char numbers[] = "0123456789".toCharArray();
    char specialChars[] = "~!@#$%^&*()-_=+[{]}|;:<>/?".toCharArray();

    List<Character> pwdLst = new ArrayList<Character>();
    for (int g = 0; g < 4; g++) {
        for (int z = 0; z < 1; z++) {
            if (g == 0) {
                pwdLst.add(numbers[rd.nextInt(10)]);
            } else if (g == 1) {
                pwdLst.add(lowerChars[rd.nextInt(26)]);
            } else if (g == 2) {
                pwdLst.add(upperChars[rd.nextInt(26)]);
            } else if (g == 3) {
                pwdLst.add(specialChars[rd.nextInt(26)]);
            }
        }
        if (pwdLst.size() == n) {
            break;
        }
        if (g + 1 == 4) {
            g = (int) Math.random() * 5;

        }
    }
    StringBuilder password = new StringBuilder();
    Collections.shuffle(pwdLst);
    for (int c = 0; c < pwdLst.size(); c++) {
        password.append(pwdLst.get(c));
    }
    return password.toString();
}

From source file:com.nearinfinity.mele.MeleBase.java

private static File getNewLocalPath(List<String> pathList, String directoryCluster, String directoryName,
        Random random) {
    Collection<String> attempts = new HashSet<String>();
    while (true) {
        if (attempts.size() == pathList.size()) {
            throw new RuntimeException("no local writable dirs");
        }//from  www. j  ava 2s  .  co m
        int index = random.nextInt(pathList.size());
        String pathname = pathList.get(index);
        attempts.add(pathname);
        File file = new File(pathname);
        file.mkdirs();
        File testFile = new File(file, UUID.randomUUID().toString());
        try {
            if (testFile.createNewFile()) {
                testFile.delete();
                File dirFile = new File(new File(file, directoryCluster), directoryName);
                dirFile.mkdirs();
                return dirFile;
            }
        } catch (IOException e) {
            LOG.error("Can not create file on [" + file.getAbsolutePath() + "]");
        }
    }
}

From source file:com.idrene.emefana.repositories.RepositoriesTest.java

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

    // NOTE: 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:fr.cph.chicago.util.Util.java

public static int getRandomColor() {
    final Random random = new Random();
    final List<TrainLine> keys = Collections.unmodifiableList(Arrays.asList(TrainLine.values()));
    return keys.get(random.nextInt(keys.size())).getColor();
}

From source file:de.uniwue.info6.misc.StringTools.java

/**
 *
 *
 * @param original//  w  ww.  j a v a 2 s.c  o m
 * @return
 */
public static String forgetOneWord(final String original) {
    final String[] originalParts = original.split("\\s");
    final Random random = new Random();
    final String originalPart = originalParts[random.nextInt(originalParts.length - 1)];
    final int index = original.indexOf(originalPart);
    return original.substring(0, index) + original.substring(index + originalPart.length(), original.length());
}

From source file:com.xidu.framework.common.util.Utils.java

public static String randomString(int length) {
    Random randGen = null;
    char[] numbersAndLetters = null;
    char[] randBuffer = new char[length];
    randGen = new Random();
    numbersAndLetters = ("abcdefghijklmnopqrstuvwxyz").toCharArray();
    int i = 0;//  www .  jav a2 s . co m
    for (i = 0; i < randBuffer.length; i++) {
        randBuffer[i] = numbersAndLetters[randGen.nextInt(26)];
    }
    return new String(randBuffer);
}