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 static int[] generateUnsortedArray(int length, int maxValue) {
    int[] array = new int[length];
    Random r = new Random();
    for (int i = 0; i < length; i++) {
        array[i] = r.nextInt(maxValue) + 1; // Avoid 0 value for better drawing
    }/*from ww w . jav a 2s  .com*/
    return array;
}

From source file:Main.java

public static String getRandomNum() {
    StringBuffer sb = new StringBuffer();
    Random random = new Random();
    for (int i = 0; i < 3; i++) {
        sb.append(random.nextInt(9));
    }//  w w w. ja  v a2  s . c o m
    return sb.toString();
}

From source file:Main.java

public static int[] randNum(int num, int min, int max) {
    ArrayList<Integer> list = new ArrayList<Integer>();
    Random rand = new Random();
    while (true) {
        int rm = (rand.nextInt(max - min) + min);
        if (!list.contains(rm)) {
            list.add(rm);/*from  ww  w.j  a  v a 2 s.c om*/
            if (list.size() >= num)
                break;
        }
    }
    int result[] = new int[num];
    for (int i = 0; i < list.size(); i++) {
        result[i] = list.get(i);
    }
    return result;
}

From source file:Main.java

public static void shuffleArray(int[] array, Random rand) {

    for (int i = array.length; i > 1; i--) {
        int b = rand.nextInt(i);
        if (b != i - 1) {
            int e = array[i - 1];
            array[i - 1] = array[b];//from w ww .j ava  2s .  c om
            array[b] = e;
        }
    }

}

From source file:Main.java

public static int getRandomInt(int sek, int min, int max) {

    Random random = new Random();

    int temp = 0;

    do {//from   w w w  .j  ava 2s.  co m
        temp = random.nextInt(sek);
    } while (temp < min || temp > max);

    return temp;
}

From source file:Main.java

public static void changeRingtone(Context context) {
    SharedPreferences sharedPreferences = context.getSharedPreferences("randomizer", Context.MODE_PRIVATE);

    if (!sharedPreferences.getBoolean("active", false)) {
        return;/*w  w w .ja  v  a2  s  .  c  o  m*/
    } // END if

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

    int count = random.nextInt(ringtoneManager.getCursor().getCount());

    RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE,
            ringtoneManager.getRingtoneUri(count));
}

From source file:com.zb.app.common.util.SerialNumGenerator.java

private static String getSuffix(Long id) {
    Long num = 1l;// w ww . ja va2 s.  c om
    Random random = new Random();
    if (id != null) {
        num = id * (random.nextInt(1000));
        if (num < 100) {
            num = num * (random.nextInt());
        }
        num = (id * (random.nextInt(1000))) % 100000;// id??
    }

    StringBuffer suffixBuffer = new StringBuffer();
    suffixBuffer.append(String.valueOf(num)).append(random.nextInt(1000));
    return StringUtils.leftPad(suffixBuffer.toString(), 8, '0');
}

From source file:Main.java

public static int[] randomSort(int[] seed) {
    int len = seed.length;
    int[] result = new int[len];
    Random random = new Random();
    for (int i = 0; i < len; i++) {
        int r = random.nextInt(len - i);
        result[i] = seed[r];//w  ww.  ja va  2  s.  c  om
        seed[r] = seed[len - 1 - i];
    }
    return result;
}

From source file:Main.java

public static String getSixRandom() {
    String str = "";
    Random random = new Random();
    for (int i = 0; i < 6; i++) {
        int itmp = random.nextInt(26) + 65;
        char ctmp = (char) itmp;
        str += ctmp;//from  w w w . ja  v a 2 s. com
    }
    return str;
}

From source file:Main.java

public static <E> void shuffleInPlace(E[] elems, Random rand) {
    for (int j = elems.length - 1; j > 0; j--) {
        int randIndex = rand.nextInt(j + 1);
        E tmp = elems[randIndex];/*from   w ww .j  a va 2 s . c om*/
        elems[randIndex] = elems[j];
        elems[j] = tmp;
    }
}