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.beligum.core.utils.Toolkit.java

public static String createRandomString(Integer length) {
    Random random = new Random();
    String name = "";
    for (int i = 0; i < length; i++) {
        int pos = random.nextInt(VALID_CHARACTERS.length());
        name += VALID_CHARACTERS.substring(pos, pos + 1);
    }// www .  ja v  a 2s . c o m
    return name;
}

From source file:com.creapple.tms.mobiledriverconsole.utils.MDCUtils.java

/**
 * Randomise int number within range/*ww w.  j a va2s. co m*/
 * @param min
 * @param max
 * @return
 */
public static int getRandomNumberInRange(int min, int max) {

    if (min >= max) {
        throw new IllegalArgumentException("max must be greater than min");
    }

    Random r = new Random();
    return r.nextInt((max - min) + 1) + min;
}

From source file:net.sf.translate64.util.TranslateUtils.java

/**
 * Gets a random funny message.//from  w  w w  . j a v a  2  s .c o m
 * @return The random message.
 */
public static String getMessage() {

    // the messages
    String[] messages = { "Every time you do this, a kitten dies.", "Your TV is lonely right now.",
            "Heeeeeere fishy, fishy, fishy!", "Don't feel sad, don't feel glue, Einstein was ugly too.",
            "Love me or leave me. Hey, where is everybody going?",
            "Roses are red,\nviolets are blue,\n most poems ryhm,\nbut this one doesn't.", "Heeere's Johnny!",
            "There are three kinds of people: those who can count and those who can't.",
            "The beatings will continue until morale improves.",
            "Copywight 1994 Elmer Fudd. All wights wesewved.", "2 + 2 = 5 for extremely large values of 2.",
            "This is not a bug, is a random feature.", "I took an IQ test and the results were negative." };

    // create a random generator
    Random randomGenerator = new Random();

    // return a random message
    return messages[randomGenerator.nextInt(messages.length)];
}

From source file:com.glaf.core.config.SystemConfig.java

public static String getToken() {
    if (TOKEN != null) {
        return TOKEN;
    }// w  w  w. ja v  a2s. c  o m
    ISystemPropertyService systemPropertyService = ContextFactory.getBean("systemPropertyService");
    SystemProperty property = systemPropertyService.getSystemPropertyById("TOKEN");
    if (property != null && property.getValue() != null) {
        TOKEN = property.getValue();
    } else {
        java.util.Random random = new java.util.Random();
        TOKEN = Math.abs(random.nextInt(9999)) + com.glaf.core.util.UUID32.getUUID()
                + Math.abs(random.nextInt(9999));
        property = new SystemProperty();
        property.setId("TOKEN");
        property.setCategory("SYS");
        property.setName("TOKEN");
        property.setLocked(0);
        property.setValue(TOKEN);
        property.setTitle("TOKEN");
        property.setType("String");
        systemPropertyService.save(property);
    }
    return TOKEN;
}

From source file:com.klwork.common.utils.WebUtils.java

/**
 * ?/*from ww  w .  j  av a  2 s .co m*/
 * @param fc
 * @param bc
 * @return
 */
public static Color getRandColor(int fc, int bc) {
    Random random = new Random();
    if (fc > 255)
        fc = 255;
    if (bc > 255)
        bc = 255;
    int r = fc + random.nextInt(bc - fc);
    int g = fc + random.nextInt(bc - fc);
    int b = fc + random.nextInt(bc - fc);
    return new Color(r, g, b);
}

From source file:uk.dsxt.voting.tests.TestDataGenerator.java

private static int randomInt(int baseMinValue, int baseMaxValue) {
    Random random = new Random();
    return baseMinValue + random.nextInt(baseMaxValue - baseMinValue + 1);
}

From source file:at.illecker.hama.hybrid.examples.testrootbeer.TestRootbeerHybridBSP.java

private static void prepareInput(Configuration conf, Path inputPath, int n, int maxVal) throws IOException {
    FileSystem fs = inputPath.getFileSystem(conf);

    // Create input file writers depending on bspTaskNum
    int bspTaskNum = conf.getInt("bsp.peers.num", 1);
    SequenceFile.Writer[] inputWriters = new SequenceFile.Writer[bspTaskNum];
    for (int i = 0; i < bspTaskNum; i++) {
        Path inputFile = new Path(inputPath, "input" + i + ".seq");
        LOG.info("inputFile: " + inputFile.toString());
        inputWriters[i] = SequenceFile.createWriter(fs, conf, inputFile, IntWritable.class, IntWritable.class,
                CompressionType.NONE);/* w  w w.  ja v  a  2s. c om*/
    }

    // Write random values to input files
    IntWritable key = new IntWritable();
    IntWritable value = new IntWritable();
    Random r = new Random();
    for (int i = 0; i < n; i++) {
        key.set(i);
        value.set(r.nextInt(maxVal));
        for (int j = 0; j < inputWriters.length; j++) {
            inputWriters[j].append(key, value);
        }
    }

    // Close file writers
    for (int j = 0; j < inputWriters.length; j++) {
        inputWriters[j].close();
    }
}

From source file:com.datumbox.common.utilities.PHPfunctions.java

/**
 * Shuffles the array values//w  w  w. j  a va 2s .c  o m
 * 
 * @param <T>
 * @param array 
 */
public static <T> void shuffle(T[] array) {
    //Implementing FisherYates shuffle
    Random rnd = RandomValue.randomGenerator;
    T tmp;
    for (int i = array.length - 1; i > 0; --i) {
        int index = rnd.nextInt(i + 1);

        // Simple swap
        tmp = array[index];
        array[index] = array[i];
        array[i] = tmp;
    }
}

From source file:com.networknt.client.ClientTest.java

private static int randInt(int min, int max) {
    Random rand = new Random();
    int randomNum = rand.nextInt((max - min) + 1) + min;
    return randomNum;
}

From source file:io.cortical.rest.model.TestDataMother.java

/**
 * Create dummy  {@link Fingerprint}./*  www.j  a  va 2  s  .c o  m*/
 * 
 * @return dummy fingerprint.
 */
public static Fingerprint createFingerprint() {
    Random random = new Random();
    Set<Integer> positionSet = new HashSet<>();
    while (positionSet.size() <= FINGERPRINT_LENGTH) {
        positionSet.add(random.nextInt(MAX_POSITION));
    }

    Integer[] positionsInteger = new Integer[FINGERPRINT_LENGTH];
    positionsInteger = positionSet.toArray(positionsInteger);
    sort(positionsInteger);
    return new Fingerprint(ArrayUtils.toPrimitive(positionsInteger));
}