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.minecraftforge.fml.common.registry.VillagerRegistry.java

/**
 * Hook called when spawning a Villager, sets it's profession to a random registered profession.
 *
 * @param entity The new entity/*from  ww  w .  j av a2  s.  c om*/
 * @param rand The world's RNG
 */
public static void setRandomProfession(EntityVillager entity, Random rand) {
    Set<String> entries = INSTANCE.professions.getKeys();
    int prof = rand.nextInt(entries.size());
    //TODO: Grab id range from internal registry
    entity.setProfession(rand.nextInt(5));
}

From source file:com.gm.machine.util.CommonUtils.java

public static String getrannumber() {
    StringBuffer strbufguess = new StringBuffer();
    String strguess = new String();
    int[] nums = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    Random rannum = new Random();
    int count;/*from   w ww .  ja  v  a2 s .c o  m*/
    int i = 0, temp_i = 0;
    for (int j = 10; j > 4; j--) {
        i = 0;
        temp_i = 0;
        count = rannum.nextInt(j);
        while (i <= count) {
            if (nums[temp_i] == -1) {
                temp_i++;
            } else {
                i++;
                temp_i++;
            }
        }
        strbufguess.append(Integer.toString(nums[temp_i - 1]));
        nums[temp_i - 1] = -1;
    }
    strguess = strbufguess.toString();
    rannum = null;
    strbufguess = null;
    nums = null;
    return strguess;
}

From source file:com.oltpbenchmark.util.CollectionUtil.java

/**
 * Return a random value from the given Collection
 * @param <T>/*from  w w  w.j ava2  s  .com*/
 * @param items
 * @param rand
 * @return
 */
public static <T> T random(Collection<T> items, Random rand) {
    int idx = rand.nextInt(items.size());
    return (CollectionUtil.get(items, idx));
}

From source file:Main.java

public static int handleRange(int i1, int i2) {
    Random rand = new Random();
    int result;/*ww  w  .  ja  v  a  2 s .  c o m*/
    int range;
    int loc;
    if (Double.valueOf(i1) > Double.valueOf(i2)) {
        range = (int) ((Double.valueOf(i1) * 100) - (Double.valueOf(i2) * 100));
        loc = (int) (Double.valueOf(i2) * 100);
    } else {
        range = (int) ((Double.valueOf(i2) * 100) - (Double.valueOf(i1) * 100));
        loc = (int) (Double.valueOf(i1) * 100);
    }
    result = (loc + rand.nextInt(range + 1)) / 100;
    return result;
}

From source file:Main.java

public static <T> List<T> getRandomDistinctElements(final Random random, final List<T> list,
        final int nElements) {
    if (list.size() < nElements)
        throw new IllegalArgumentException("cannot sample " + nElements + " elements from " + list.size());

    // TODO: avoid creating "bowl" collection
    final List<T> bowl = new ArrayList<T>(list);
    final List<T> sample = new ArrayList<T>(nElements);

    for (int i = 0; i < nElements; i++) {
        sample.add(bowl.remove(random.nextInt(bowl.size())));
    }// w  ww  .  j  av  a2 s.co m

    return sample;
}

From source file:com.sonicle.webtop.core.util.IdentifierUtils.java

/**
 * @deprecated use com.sonicle.commons.IdentifierUtils.getRandomAlphaNumericString instead
 * @return/*from w  w w. j av  a  2 s  . c o  m*/
 */
@Deprecated
public static synchronized String getRandomAlphaNumericString(int length) {
    try {
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        Random rand = new Random();
        char buff[] = new char[length];
        for (int i = 0; i < length; ++i) {
            // reseed rand once you've used up all available entropy bits
            if ((i % 10) == 0)
                rand.setSeed(sr.nextLong()); // 64 bits of random!
            buff[i] = VALID_CHARACTERS[rand.nextInt(VALID_CHARACTERS.length)];
        }
        return new String(buff);
    } catch (NoSuchAlgorithmException ex) {
        return null;
    }
}

From source file:ala.soils2sat.DrawingUtils.java

public static Color generateRandomColor(Color mix) {
    Random random = new Random();
    int red = random.nextInt(256);
    int green = random.nextInt(256);
    int blue = random.nextInt(256);

    // mix the color
    if (mix != null) {
        red = (red + mix.getRed()) / 2;//from w  ww .  ja  v a  2 s .  c om
        green = (green + mix.getGreen()) / 2;
        blue = (blue + mix.getBlue()) / 2;
    }

    Color color = new Color(red, green, blue);
    return color;
}

From source file:io.druid.query.aggregation.datasketches.tuple.GenerateTestData.java

private static void generateSketches() throws Exception {
    Path path = FileSystems.getDefault().getPath("array_of_doubles_sketch_data.tsv");
    try (BufferedWriter out = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
        Random rand = new Random();
        int key = 0;
        for (int i = 0; i < 20; i++) {
            ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder()
                    .setNominalEntries(1024).build();
            sketch.update(key++, new double[] { 1 });
            sketch.update(key++, new double[] { 1 });
            out.write("2015010101");
            out.write('\t');
            out.write("product_" + (rand.nextInt(10) + 1));
            out.write('\t');
            out.write(Base64.encodeBase64String(sketch.compact().toByteArray()));
            out.newLine();/*from   w w  w.j  a v  a2s .c  o m*/
        }
    }
}

From source file:org.imsglobal.lti.LTIUtil.java

/**
* Generates a random string.//w  w w  .j a va 2  s.com
* <p>
* The generated string will only comprise letters (upper- and lower-case) and digits.
*
* @param length Length of string to be generated
*
* @return random string
*/
protected static String getRandomString(int length) {

    String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    Random rand = new Random();
    StringBuilder value = new StringBuilder();
    int charsLength = chars.length() - 1;

    for (int i = 0; i < length; i++) {
        value.append(chars.charAt(rand.nextInt(charsLength)));
    }

    return value.toString();

}

From source file:at.illecker.hama.hybrid.examples.summation.SummationBSP.java

static BigDecimal writeSummationInputFile(FileSystem fs, Path dir, int fileCount) throws IOException {

    BigDecimal sum = new BigDecimal(0);
    Random rand = new Random();
    double rangeMin = 0;
    double rangeMax = 100;

    for (int i = 0; i < fileCount; i++) {
        DataOutputStream out = fs.create(new Path(dir, "part" + i));

        // loop between 50 and 149 times
        for (int j = 0; j < rand.nextInt(100) + 50; j++) {
            // generate key value pair inputs
            double randomValue = rangeMin + (rangeMax - rangeMin) * rand.nextDouble();

            String truncatedValue = new BigDecimal(randomValue)
                    .setScale(DOUBLE_PRECISION, BigDecimal.ROUND_DOWN).toString();

            String line = "key" + (j + 1) + "\t" + truncatedValue + "\n";
            out.writeBytes(line);/*from w w  w.  j ava  2  s .c om*/

            sum = sum.add(new BigDecimal(truncatedValue));
            LOG.debug("input[" + j + "]: '" + line + "' sum: " + sum.toString());
        }
        out.close();
    }
    return sum;
}