Example usage for java.util Random Random

List of usage examples for java.util Random Random

Introduction

In this page you can find the example usage for java.util Random Random.

Prototype

public Random() 

Source Link

Document

Creates a new random number generator.

Usage

From source file:Main.java

public static int getRandomArgb(int alpha, int begin, int last) {
    if (last > 255) {
        last = 255;/*from  w  ww  . ja  v a 2  s . co  m*/
    }
    int red = begin + new Random().nextInt(last - begin);
    int green = begin + new Random().nextInt(last - begin);
    int blue = begin + new Random().nextInt(last - begin);
    return Color.argb(alpha, red, green, blue);
}

From source file:Main.java

public static int rollDice(int number, int nSides) {
    int num = 0;/*www .  j  av  a2s . c om*/
    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;
}

From source file:Main.java

/**
 * random in array/* w w w.  ja  v  a 2 s.  com*/
 *
 * @param array array which want to random
 * @return return a index of member in array
 */
public static int getRandom(int[] array) {
    return new Random().nextInt(array.length);
}

From source file:Main.java

public static int randInt(int min, int max) {

    // Usually this should be a field rather than a method variable so
    // that it is not re-seeded every call.
    Random rand = new Random();

    // nextInt is normally exclusive of the top value,
    // so add 1 to make it inclusive
    int randomNum = rand.nextInt((max - min) + 1) + min;

    return randomNum;
}

From source file:Main.java

public static String getRandomString(int length) {
    String str = "abcdefghigklmnopkrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ0123456789";
    Random random = new Random();
    StringBuffer sf = new StringBuffer();
    for (int i = 0; i < length; i++) {
        int number = random.nextInt(62);//0~61
        sf.append(str.charAt(number));//from   w  w w  .  j  av a 2s .  com

    }
    return sf.toString();
}

From source file:Main.java

public static <T> ArrayList<T> rand(ArrayList<T> population, int nSamplesNeeded) {
    Random r = new Random();
    ArrayList<T> ret = new ArrayList<T>();

    if (nSamplesNeeded > population.size() / 2) {
        ArrayList<T> original = new ArrayList<T>();
        original = population;//from   w  w w. j a  v a2s .  c  o m

        while (nSamplesNeeded > 0) {
            int rand = r.nextInt(original.size());
            if (rand < nSamplesNeeded) {
                ret.add(original.get(rand));
                original.remove(rand);
                nSamplesNeeded--;
            }
        }
        original.clear();
    } else
        ret = shuffle(population, nSamplesNeeded);

    return ret;
}

From source file:Main.java

/**
 * use and random value,maybe this can provide good experince
 *
 * @param min//from  w  ww. jav  a  2s .co  m
 * @param max
 * @return
 */
public static int getRandom(int min, int max) {
    Random random = new Random();
    return random.nextInt(Math.abs(max - min)) + min;
}

From source file:Main.java

public static String randomString(int stringLength) {
    char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    StringBuilder randomStringBuilder = new StringBuilder();
    Random random = new Random();
    for (int i = 0; i < stringLength; i++) {
        char c = chars[random.nextInt(chars.length)];
        randomStringBuilder.append(c);//  ww  w.j a  v  a  2  s. c  om
    }
    return randomStringBuilder.toString();
}

From source file:Main.java

public static int getRandom(int minNumber, int maxNumber) {
    Random rand = new Random();
    int randomNum = rand.nextInt(maxNumber - minNumber);
    return randomNum;
}

From source file:Main.java

public static String generateFilenameString() {
    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
    return df.format(new Date()) + "_" + new Random().nextInt(1000);
}