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

public synchronized static String getRandomCode(int length) {
    String sRand = "";
    for (int i = 0; i < length; i++) {
        Random random = new Random();
        String rand = String.valueOf(random.nextInt(10));
        sRand += rand;/*w  w w.  java  2  s  .c  o m*/
    }
    return sRand;
}

From source file:Main.java

public static String getText(Document doc, String tagName, boolean flag) {

    NodeList nodeList = doc.getElementsByTagName(tagName);
    int leng = nodeList.getLength();
    if (leng <= 0)
        return "";

    Random rnd = new Random();
    int ran = rnd.nextInt(leng);

    return nodeList.item(ran).getTextContent();
}

From source file:Main.java

/**
 * Get a random color//from ww w.  java  2s  .c  o  m
 *
 * @return Random color
 */
public static int getRandomColor() {
    Random random = new Random();
    return Color.rgb(random.nextInt(sColor_Range), random.nextInt(sColor_Range), random.nextInt(sColor_Range));
}

From source file:cz.cuni.mff.d3s.spl.example.newton.app.Main.java

private static double[] generateCoefficients(Random random) {
    int n = random.nextInt(MAX_POLYNOM_DEGREE) + 1;
    double[] result = new double[n];
    for (int i = 0; i < n; i++) {
        double coeff = random.nextDouble() - 0.5;
        result[i] = coeff * COEFFICIENT_MULTIPLIER;
    }//from   w w w. j av a2 s . com
    return result;
}

From source file:Main.java

public static int generateAutomaticRoll() {
    Random random = new Random();
    int low = 1;/*from   www.j  a v  a2  s.c om*/
    int high = 100;
    return random.nextInt(high - low) + low;
}

From source file:Main.java

public static int randomInt(int min, int max) {
    Random random = new Random(System.currentTimeMillis());
    return min + random.nextInt(max - min);
}

From source file:Main.java

public static void changeRingtone(Context context) {

    SharedPreferences preferences = context.getSharedPreferences("randomizer", Context.MODE_PRIVATE);
    if (!preferences.getBoolean("active", false))
        return;//from   ww w .  ja va2s.co m

    RingtoneManager mgr = new RingtoneManager(context);
    Random random = new Random(System.currentTimeMillis());

    int n = random.nextInt(mgr.getCursor().getCount());

    RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, mgr.getRingtoneUri(n));
}

From source file:Main.java

public static String get_Random_Color() {
    String[] arr = { "#f44336", "#ff4081", "#ff5722", "#ff9800", "#03a9f4", "#009688", "#388e3c" };
    Random rand = new Random();
    int max = 5;//from  w  w  w. j  ava  2  s .c  o  m
    int min = 0;
    int num = rand.nextInt((max - min) + 1 + min);
    return arr[num];
}

From source file:Main.java

private static String generateRandomCharacter() {
    final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    final int length = alphabet.length();
    Random random = new Random();
    String randomChar = String.valueOf(alphabet.charAt(random.nextInt(length)));
    return randomChar;
}

From source file:Main.java

public static int rollDice(int number, int nSides) {
    int num = 0;/*from   w  w  w .  j  a va 2  s.co  m*/
    int roll = 0;
    Random r = new Random();
    if (nSides >= 3) {
        for (int i = 0; i < number; i++) {
            roll = r.nextInt(nSides) + 1;
            System.out.println("Roll is:  " + roll);
            num = num + roll;
        }
    } else {
        System.out.println("Error num needs to be from 3");
    }
    return num;
}