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:Main.java

private static String randomMac() {
    String chars = "abcde0123456789";
    String res = "";
    Random rnd = new Random();
    int leng = chars.length();
    for (int i = 0; i < 17; i++) {
        if (i % 3 == 2) {
            res = res + ":";
        } else {/*  w ww . java2s . c o m*/
            res = res + chars.charAt(rnd.nextInt(leng));
        }

    }
    return res;
}

From source file:com.gst.infrastructure.documentmanagement.contentrepository.ContentRepositoryUtils.java

/**
 * Generate a random number between 5 to 16
 * /*from   w w w  .ja  v a  2s .  c  o  m*/
 * @return
 */
public static int generateRandomNumber() {
    final Random randomGenerator = new Random();
    return randomGenerator.nextInt(11) + 5;
}

From source file:Main.java

private static String genRandomString(int pLength) {
    Random r = new Random();
    String letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < pLength; i++) {
        sb.append(letter.charAt(r.nextInt(1000000) % (letter.length())));
    }/*ww  w.j  a  v a 2  s.co m*/
    return sb.toString();
}

From source file:Main.java

public static String getRandomUserId(int i) {
    Random random = new Random();
    StringBuilder stringbuilder = new StringBuilder(i);
    int j = 0;//from  w  w  w .  ja v a 2 s .  com
    do {
        if (j >= i)
            return stringbuilder.toString();
        stringbuilder.append((char) (48 + random.nextInt(10)));
        j++;
    } while (true);
}

From source file:com.linkedin.paldb.impl.GenerateTestData.java

public static Integer[] generateIntData(int count) {
    Integer[] res = new Integer[count];
    Random random = new Random(count + 34593263544354353l);
    for (int i = 0; i < count; i++) {
        res[i] = random.nextInt(1000000);
    }/* w  w  w  .  j  a va 2s.c  o  m*/
    return res;
}

From source file:graphgen.GraphGen.java

private static String generateGraph(int nodeCount, int edgeCount) {
    boolean edgesArray[][] = new boolean[nodeCount + 1][nodeCount + 1];
    Random rand = new Random();

    StringBuilder graphString = new StringBuilder("graph G {\n");
    int counter = 0;
    for (int i = 2; i <= nodeCount; i++) {
        int randomNode = rand.nextInt(i - 1) + 1;
        if (!edgeExists(edgesArray, i, randomNode)) {
            edgesArray[i][randomNode] = true;
            graphString.append(i).append(" -- ").append(randomNode).append(";\n");
            counter++;//from   w  w  w.  j  a v a2 s .c  o m
        }
    }
    while (counter < edgeCount) {
        int rN1 = rand.nextInt(nodeCount) + 1;
        int rN2 = rand.nextInt(nodeCount) + 1;
        if (!edgeExists(edgesArray, rN1, rN2)) {
            edgesArray[rN1][rN2] = true;
            graphString.append(rN1).append(" -- ").append(rN2).append(";\n");
            counter++;
        }

    }
    graphString.append("}");

    return graphString.toString();

}

From source file:Main.java

/**
 * Fills the array with random ints.  Values will be between min (inclusive) and
 * max (inclusive)./*  w  ww  .  j a va  2  s .co  m*/
 */
public static void genRandomInts(long seed, int min, int max, int array[]) {
    Random r = new Random(seed);
    for (int i = 0; i < array.length; i++) {
        long range = max - min + 1;
        array[i] = (int) (min + r.nextLong() % range);
    }
    array[r.nextInt(array.length)] = min;
    array[r.nextInt(array.length)] = max;
}

From source file:ch.ethz.inf.vs.android.g54.a4.util.SnapshotCache.java

/**
 * Returns snapshot at index modulo the length of the list, if index is < 0, it returns a random snapshot
 */// w w w  .j a  va2s.c o m
private static List<WifiReading> getSnapshot(int index, Context c) {
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can at least read the external storage
        File root = c.getExternalCacheDir();
        File[] snapshotFiles = root.listFiles(new FilenameFilter() {

            public boolean accept(File dir, String filename) {
                if (filename.endsWith(FILE_EXTENSION)) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        if (snapshotFiles.length > 0) {
            if (index < 0) {
                Random rand = new Random();
                index = rand.nextInt(snapshotFiles.length);
            } else {
                index = index % snapshotFiles.length;
            }
            try {
                // read file into a string
                FileInputStream fstream = new FileInputStream(snapshotFiles[index]);
                Log.d(TAG, snapshotFiles[index].getName());
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String fileContents = "";
                String strLine;
                while ((strLine = br.readLine()) != null) {
                    fileContents += strLine;
                }

                // make a json array out of the string
                JSONArray json = new JSONArray(fileContents);

                // parse the json array
                return jsonToReadings(json);
            } catch (Exception e) {
                Log.e(TAG, "Could not read file.");
                return null;
            }
        } else {
            // there are no cached snapshots
            return null;
        }
    } else {
        // we cannot read the external storage
        return null;
    }
}

From source file:com.embeddedlapps.primeraversion.ListAdapterHolder.java

/**
 * Create Random Users/*from w w  w .j  a  v  a2 s .  co  m*/
 */
/*
private void createUserDetails() {
for (int i = 0; i < 100; i++) {
    final UserDetails mDetails = new UserDetails();
    mDetails.setName("Name " + i);
    mDetails.setIdIMagen(R.drawable.one);
    mDetails.setIdIMagen((i % 2) == 0 ? R.drawable.two : R.drawable.one);
        
    mUserDetails.add(mDetails);
}
}
*/
/*
 * Snippet from http://stackoverflow.com/a/363692/1008278
 */
public static int randInt(int min, int max) {
    final Random rand = new Random();
    return rand.nextInt((max - min) + 1) + min;
}

From source file:com.linkedin.paldb.impl.GenerateTestData.java

public static Object[] generateCompoundKeys(int count) {
    Object[] res = new Object[count];
    Random random = new Random(345);
    for (int i = 0; i < count; i++) {
        Object[] k = new Object[] { new Byte((byte) random.nextInt(10)), new Integer(i) };
        res[i] = k;/*from  w  w w.j a v  a2 s  . c  o m*/
    }
    return res;
}