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:local.apache.MultipartEntity.java

/**
 * Generates a random multipart boundary string.
*//*from w  ww. j av a2  s .  com*/
private static byte[] generateMultipartBoundary() {
    final Random rand = new Random();
    final byte[] bytes = new byte[rand.nextInt(11) + 30]; // a random size from 30 to 40 // NOSONAR ignore magic numbers.
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)];
    }
    return bytes;
}

From source file:gt.dakaik.common.Common.java

public static String getToken() {
    Random rnd = new Random();
    StringBuilder sb = new StringBuilder(32);
    for (int i = 0; i < 32; i++) {
        sb.append(TOKEN.charAt(rnd.nextInt(TOKEN.length())));
    }/*w ww  .ja v  a2s  .c o  m*/

    return sb.toString();
}

From source file:Arrays.java

/**
 * Randomly permutes the elements of the specified array using the
 * specified randomizer.  The resulting array will have the same
 * elements, but arranged into a (possibly) different order.
 *
 * @param xs Array to permute./*w w  w  . ja v  a 2 s  .  c om*/
 * @param random Randomizer to use for permuation.
 * @param <E> the type of objects in the array being permuted
 */
public static <E> void permute(E[] xs, Random random) {
    for (int i = xs.length; --i > 0;) {
        int pos = random.nextInt(i);
        E temp = xs[pos];
        xs[pos] = xs[i];
        xs[i] = temp;
    }
}

From source file:Arrays.java

/**
 * Randomly permutes the elements of the specified integer
 * array using the specified randomizer.
 *
 * @param xs Array to permute./*from  w w w .  ja  v a 2  s  .co  m*/
 * @param random Randomizer to use for permutations.
 */
public static void permute(int[] xs, Random random) {
    for (int i = xs.length; --i > 0;) {
        int pos = random.nextInt(i);
        int temp = xs[pos];
        xs[pos] = xs[i];
        xs[i] = temp;
    }
}

From source file:Main.java

private static String generateNonce(int length) {
    Random random = new Random(System.currentTimeMillis());
    if (length < 10)
        length = 10;//from ww  w . j  av a 2s . c  o m

    int MAX_LEN = NONCE_SAMPLE.length();
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < length; i++) {
        buf.append(NONCE_SAMPLE.charAt(random.nextInt(MAX_LEN)));
    }
    return buf.toString();
}

From source file:it.cnr.isti.smartfed.test.DatacenterFacilities.java

public static List<FederationDatacenter> getNormalDistribution(int numOfDatacenters, int numHost) {
    Random r = new Random(13213);

    int core_variance = maxNumOfCores - minNumOfCores;
    int delta_cores = core_variance > 0 ? r.nextInt(core_variance) : 0;

    List<FederationDatacenter> list = new ArrayList<FederationDatacenter>();
    NormalDistribution nd = new NormalDistribution(numOfDatacenters / 2d, numOfDatacenters / 4d);

    // System.out.println("Aa"+numHost);

    for (int i = 0; i < numOfDatacenters; i++) {
        // create the virtual processor (PE)
        List<Pe> peList = new ArrayList<Pe>();
        int mips = 25000;
        for (int j = 0; j < minNumOfCores + delta_cores; j++) {
            peList.add(new Pe(j, new PeProvisionerSimple(mips)));
        }//  w  w  w.  j av  a  2s .c  o m

        // create the hosts
        List<Host> hostList = new ArrayList<Host>();
        HostProfile prof = HostProfile.getDefault();
        prof.set(HostParams.RAM_AMOUNT_MB, 16 * 1024 + "");

        int num;
        if (numOfDatacenters == 1) {
            num = numHost;
        } else {
            Double value = new Double(nd.density(i)) * numHost;
            num = value.intValue();
        }

        if (num < 1)
            num = 1;

        for (int k = 0; k < num; k++) {
            hostList.add(HostFactory.get(prof, peList));
        }

        // create the storage
        List<Storage> storageList = new ArrayList<Storage>(); // if empty, no SAN attached

        // create the datacenters
        list.add(FederationDatacenterFactory.getDefault(hostList, storageList));
    }

    return list;
}

From source file:de.tudarmstadt.ukp.dkpro.core.performance.PerformanceTestUtil.java

/**
 * Initializes a CAS with random text, tokens, and sentences.
 * /*from  www.j a  va  2 s. c  o  m*/
 * @param aJCas the CAS
 * @param aTextSize the length of the text to be generated.
 * @param aAnnotationCount the number of annotations to be generated.
 * @param aSeed the random seed to allow for repeatable randomness.
 */
public static void initRandomCas(JCas aJCas, int aTextSize, int aAnnotationCount, long aSeed) {
    List<Type> types = new ArrayList<Type>();
    types.add(getType(aJCas, Token.class));
    types.add(getType(aJCas, Sentence.class));
    //        Iterator<Type> i = aJCas.getTypeSystem().getTypeIterator();
    //        while (i.hasNext()) {
    //            Type t = i.next();
    //            if (t.isArray() || t.isPrimitive()) {
    //                continue;
    //            }
    //            if (aJCas.getDocumentAnnotationFs().getType().getName().equals(t.getName())) {
    //                continue;
    //            }
    //            types.add(t);
    //        }

    // Initialize randomizer
    Random rnd = new Random(aSeed);

    // Shuffle the types
    for (int n = 0; n < 10; n++) {
        Type t = types.remove(rnd.nextInt(types.size()));
        types.add(t);
    }

    // Generate random text
    aJCas.setDocumentText(RandomStringUtils.random(aTextSize));

    // Generate random annotations
    CAS cas = aJCas.getCas();
    for (int n = 0; n < aAnnotationCount; n++) {
        Type t = types.get(n % types.size());
        int length = rnd.nextInt(30);
        int begin = rnd.nextInt(aTextSize);
        int end = begin + length;
        if (end > aTextSize) {
            n--; // Skip and extend loop by one
            continue;
        }
        cas.addFsToIndexes(cas.createAnnotation(t, begin, end));
    }
}

From source file:eu.digitisation.idiomaident.utils.ExtractTestSamples.java

private static String nextSample(File inFolder) {
    File[] langFolders = inFolder.listFiles();

    Date now = new Date();
    Random rand = new Random(now.getTime());

    //get a ramdom language       
    int numLangs = langFolders.length;
    int chosenLang = rand.nextInt(numLangs);

    String lang = langFolders[chosenLang].getName();

    //get a ramdom file from the folder        
    File[] langFiles = langFolders[chosenLang].listFiles();
    int numFiles = langFiles.length;
    int chosenFile = rand.nextInt(numFiles);

    File langFile = langFiles[chosenFile];

    //open and read the file
    try {/*www.  j a va 2s.c  o m*/
        String text = FileUtils.readFileToString(langFile, Charset.forName("UTF-8"));

        text = StringNormalize.stringNormalize(text);

        text = text.replaceAll("[\\n.,:;]", " ");
        text = text.trim();

        if (text.replaceAll("[\\p{Space}]+", "").length() < 20) {
            return null;
        }

        //split the text in words
        String[] words = text.split("[\\p{Space}]+");

        boolean correct = false;
        StringBuilder sampleBuild = null;

        while (!correct) {
            //chosen the initial position to read the words
            int actualWord = rand.nextInt(words.length);
            correct = true;

            sampleBuild = new StringBuilder();
            while (sampleBuild.length() < 20) {
                if (actualWord < words.length) {
                    sampleBuild.append(words[actualWord]);
                    sampleBuild.append(" ");
                } else {
                    correct = false;
                    break;
                }

                actualWord++;
            }
        }

        //complete the sample
        sampleBuild.deleteCharAt(sampleBuild.length() - 1);
        sampleBuild.append(";").append(lang).append("\n");

        return sampleBuild.toString();

    } catch (IOException ex) {
        System.out.println(ex.toString());
    }

    return null;
}

From source file:de.unentscheidbar.csv2.CaseInsensitiveBenchmark.java

static final String randomString(Random rnd, int length) {

    StringBuilder b = new StringBuilder(length);
    for (int i = 0; i < length; i++) {
        b.append(safeChars.charAt(rnd.nextInt(safeChars.length())));
    }//from   w ww.j  a  v a2s .co  m
    return b.toString();
}

From source file:net.dv8tion.discord.util.GoogleSearch.java

private static String randomName(int randomLength) {
    char[] characters = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
            'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
            'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3',
            '4', '5', '6', '7', '8', '9', '0' };

    Random rand = new Random();
    StringBuilder builder = new StringBuilder();
    builder.append("DiscordBot/");
    for (int i = 0; i < randomLength; i++) {
        builder.append(characters[rand.nextInt(characters.length)]);
    }/*from   ww  w . j  a v a2 s. c  o m*/
    return builder.toString();
}