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.griddynamics.jagger.reporting.LatencyPlotReportProvider.java

public static XYSeriesCollection getSeriesCollection(int seriesCount) {
    XYSeriesCollection result = new XYSeriesCollection();
    Random rnd = new Random();
    for (int i = 0; i < seriesCount; i++) {
        XYSeries series = new XYSeries(i, false, false);
        result.addSeries(series);/*from  www . j a  v  a2s  .  c  o m*/
        for (int t = 0; t < 100; t++) {
            series.add(t, rnd.nextInt(50));
        }
    }

    return result;
}

From source file:com.linkedin.drelephant.math.Statistics.java

/**
 * Create a random sample within the original array
 *///from   w  ww  . ja va  2  s  . c o m
public static <T> void shuffleArraySample(T[] array, int sampleSize) {
    if (array.length <= sampleSize) {
        return;
    }

    T temp;
    int index;
    Random random = new Random();

    for (int i = 0; i < sampleSize; i++) {
        index = random.nextInt(array.length - i) + i;
        temp = array[index];
        array[index] = array[i];
        array[i] = temp;
    }
}

From source file:net.duckling.ddl.web.api.rest.TeamController.java

private static String getRandomString(Integer length) {
    String str = "";
    Random random = new Random();
    for (int i = 0; i < length; i++) {
        boolean b = random.nextBoolean();
        if (b) {//from   ww  w . j  a va 2s . c  om
            str += (char) (97 + random.nextInt(26));// ???  
        } else {
            str += String.valueOf(random.nextInt(10));
        }
    }
    return str;
}

From source file:com.wso2telco.gsma.authenticators.Utility.java

/**
 * Returns the OTP for defined length with minimum length should be 4
 * @param length for the otp/*from   w  ww  . j  ava 2  s  .co m*/
 */
public static String genarateOTP(int length) {
    if (length < 4) {
        length = 4;
    }
    String numbers = "0123456789";
    // Using random method
    Random rndm_method = new Random();
    char[] password = new char[length];
    for (int i = 0; i < length; i++) {
        // Use of charAt() method : to get character value
        // Use of nextInt() as it is scanning the value as int
        password[i] = numbers.charAt(rndm_method.nextInt(numbers.length()));
    }
    return String.valueOf(password);
}

From source file:de.unisb.cs.st.javalanche.mutation.run.task.RandomTaskCreator.java

public static void createTaskRandomSelection(int numberOfMutations, int seed, String fileName)
        throws IOException {
    Random r = new Random(seed);
    Set<Long> coveredSet = MutationCoverageFile.getCoveredMutations();
    List<Long> covered = new ArrayList<Long>(coveredSet);
    Set<Long> selected = new HashSet<Long>();
    List<String> lines = new ArrayList<String>();
    for (int i = 0; i < numberOfMutations && covered.size() > 0; i++) {
        int index = r.nextInt(covered.size());
        Long id = covered.get(index);
        covered.remove(index);//  ww w . j av  a 2s .co  m
        selected.add(id);
        lines.add(id + "");
    }
    // List<Mutation> mutations = QueryManager.getMutationsByIds(selected
    // .toArray(new Long[0]));
    // Collections.sort(mutations);
    File file = new File(fileName);
    FileUtils.writeLines(file, lines);

}

From source file:com.griddynamics.jagger.reporting.LatencyPlotReportProvider.java

public static List<List<Double>> getBarData() {
    Random rnd = new Random();
    List<List<Double>> range = new ArrayList<List<Double>>();
    for (int i = 0; i < 64; i++) {
        List<Double> stripe = new ArrayList<Double>();
        for (int j = 0; j < 16; j++) {
            stripe.add((double) rnd.nextInt(50) + 1);
        }//from  ww w  .j ava 2s. c o m
        range.add(stripe);
    }

    return range;
}

From source file:it.mb.whatshare.CallGooGlInbound.java

private static String getURL(String encodedId, String deviceAssignedID) {
    StringBuilder builder = new StringBuilder("http://");
    Random generator = new Random();
    int sum = 0;/*  ww w.  ja va  2 s.  c o m*/
    for (int i = 0; i < 8; i++) {
        char rand = CHARACTERS[generator.nextInt(CHARACTERS.length)];
        builder.append(rand);
        // no idea why they set lowercase for domain names...
        sum += CHAR_MAP.get(Character.toLowerCase(rand));
    }
    builder.append("/");
    builder.append(sum);
    builder.append("?model=");
    try {
        builder.append(URLEncoder
                .encode(String.format("%s %s", Utils.capitalize(Build.MANUFACTURER), Build.MODEL), "UTF-8")
                .replaceAll("\\+", "%20"));
        builder.append("&yourid=");
        builder.append(deviceAssignedID);
        builder.append("&id=");
        builder.append(encodedId);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return builder.toString();
}

From source file:at.illecker.hama.hybrid.examples.hellohybrid.HelloHybridBSP.java

private static void prepareInput(Configuration conf, Path inputPath, Path exampleFile, int n)
        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, NullWritable.class,
                CompressionType.NONE);//from   w w w .j a  v  a2s  .  c o m
    }

    // Create example file writer
    SequenceFile.Writer exampleWriter = SequenceFile.createWriter(fs, conf, exampleFile, IntWritable.class,
            NullWritable.class, CompressionType.NONE);

    // Write random values to input files and example
    IntWritable inputKey = new IntWritable();
    NullWritable nullValue = NullWritable.get();
    Random r = new Random();
    for (long i = 0; i < n; i++) {
        inputKey.set(r.nextInt(n));
        for (int j = 0; j < inputWriters.length; j++) {
            inputWriters[j].append(inputKey, nullValue);
        }
        inputKey.set(r.nextInt(n));
        exampleWriter.append(inputKey, nullValue);
    }

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

From source file:net.mceoin.cominghome.api.NestUtil.java

public static String tellNestAwayStatus(String access_token, String structure_id, String away_status) {
    String result = tellNestAwayStatusCall(access_token, structure_id, away_status);

    if ((result.contains("Error:")) && (!result.contains("Unauthorized"))) {
        // Try again if it was an Error but not an Unauthorized
        try {/*from   w  w  w  .  j a  va  2 s.c  om*/
            Random randomGenerator = new Random();
            int seconds = 5 + randomGenerator.nextInt(10);
            log.info("retry in " + seconds + " seconds");
            Thread.sleep(seconds * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        result = tellNestAwayStatusCall(access_token, structure_id, away_status);
    }

    return result;
}

From source file:net.mceoin.cominghome.api.NestUtil.java

public static String getNestAwayStatus(String access_token) {
    String result = getNestAwayStatusCall(access_token);

    if ((result.contains("Error:")) && (!result.contains("Unauthorized"))) {
        // Try again if it was an Error but not an Unauthorized
        try {/* w ww . jav a2 s  .c om*/
            Random randomGenerator = new Random();
            int seconds = 5 + randomGenerator.nextInt(10);
            log.info("retry in " + seconds + " seconds");
            Thread.sleep(seconds * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        result = getNestAwayStatusCall(access_token);
    }

    return result;
}