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

/**
 * get random number from your boundary//from   ww  w.j ava 2  s  .c  o m
 *
 * @param number max number till you want get random.
 * @return random number
 */
public static int getRandom(int number) {
    Random rand = new Random();
    return rand.nextInt(number);
}

From source file:Main.java

private static void BindPort() {
    Random random = new Random();
    port = random.nextInt(65535);//w w  w .  ja  v  a 2  s.  c  om

    try {
        datagramSocket = new DatagramSocket(port);
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        BindPort();
    }
}

From source file:Main.java

public static void sleep() {
    sleep(new Random().nextInt(2000));
}

From source file:Main.java

static void shuffleArray(int[] ar) {
    Random rnd = new Random();
    for (int i = ar.length - 1; i > 0; i--) {
        int index = rnd.nextInt(i + 1);
        // Simple swap
        int a = ar[index];
        ar[index] = ar[i];/*from w ww  . j a v a2s .  co  m*/
        ar[i] = a;
    }
}

From source file:Main.java

public static String generateToken() {
    char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
    StringBuilder sb = new StringBuilder();
    Random random = new Random();

    for (int i = 0; i < 40; i++) {
        char c = chars[random.nextInt(chars.length)];
        sb.append(c);//from   w  w  w .j  a  va2 s. c om
    }

    return sb.toString();
}

From source file:Main.java

/**
 * Select a random number within a specified range.
 * @param min Lower limit.//from   www.j  a  v  a 2 s .co  m
 * @param max Upper limit.
 * @return int
 */
public static int getRandomInteger(int min, int max) {
    Random r = new Random();
    return r.nextInt((max - min) + 1) + min;
}

From source file:Main.java

public static String[] randomizeArray(String[] array) {

    Arrays.sort(array, new Comparator<String>() {

        private final Random generator = new Random();

        @Override// w  w w  .j av a 2  s  . c  o m
        public int compare(String lhs, String rhs) {
            // returns a random int between 1 and -1
            return (generator.nextInt(1) * 2) - 1;
        }
    });
    return array;
}

From source file:Main.java

/**
 * get random color rgb value/* w w w  . ja v a 2  s. c o  m*/
 * @return rgb color int
 */
public static int getRandomColor() {
    Random random = new Random();
    /*
     * Better limiting value.
     * if value is too small, it will appear lighter
     * if value is too large, it will appear darker
     */
    //limit within 50-199
    int red = random.nextInt(150) + 50;
    int green = random.nextInt(150) + 50;
    int blue = random.nextInt(150) + 50;
    //create a color int as per rgb and return
    return Color.rgb(red, green, blue);
}

From source file:Main.java

public static void initialize_weight(float[] weight, int size, boolean randomFlag) {
    // if randomFlag == true, generate weight randomly
    // otherwise, initialize all weight to 0.0
    Random randomGenerator = new Random();
    for (int i = 0; i < size; i++) {
        if (randomFlag == true) {
            weight[i] = randomGenerator.nextFloat();
        } else {/*from  w w w  .  j  a v a2 s. c o m*/
            weight[i] = (float) 0.0;
        }
    }
}

From source file:Main.java

public static int[] getMachineSelectionNumNo(int size, int numbers[]) {
    int length = numbers.length;
    int num[] = new int[size];
    Random random = new Random();
    List<Integer> list = new ArrayList<Integer>();
    while (list.size() < size) {
        int number = numbers[random.nextInt(length)];
        list.add(number);//from  ww  w  .ja v  a  2s.  c  o  m
    }

    for (int i = 0; i < list.size(); i++) {
        num[i] = list.get(i);
    }
    return num;
}