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 void main(String args[]) {
    int a = 0, b = 0, c = 0;
    Random r = new Random();
    for (int i = 0; i < 32000; i++) {
        try {//  ww w. j  a  v  a 2  s  . co  m
            b = r.nextInt();
            c = r.nextInt();
            a = 12345 / (b / c);
        } catch (ArithmeticException e) {
            System.out.println("Exception: " + e);
            a = 0; // set a to zero and continue
        }
        System.out.println("a: " + a);
    }
}

From source file:Main.java

public static void main(String args[]) {
    int a = 0, b = 0, c = 0;
    Random r = new Random();
    for (int i = 0; i < 32000; i++) {
        try {/*from  www .j  ava  2s .  c o m*/
            b = r.nextInt();
            c = r.nextInt();
            a = 12345 / (b / c);
        } catch (ArithmeticException e) {
            System.out.println("Division by zero.");
            a = 0; // set a to zero and continue
        }
        System.out.println("a: " + a);
    }
}

From source file:Main.java

public static void main(String... a) {
    int n = 16;// w w w .j a  va 2  s.com

    Random r = new Random();
    byte[] b = new byte[n];
    r.nextBytes(b);
    BigInteger i = new BigInteger(b);

    System.out.println(i);
}

From source file:Main.java

public static void main(String[] args) {

    String[] arr = { "A", "B", "C", "D" };
    Random random = new Random();

    int select = random.nextInt(arr.length);

    System.out.println("Random String selected: " + arr[select]);
}

From source file:MainClass.java

public static void main(String args[]) {
    Random rand = new Random();
    System.out.println(rand.nextDouble());
}

From source file:Main.java

public static void main(String[] args) {
    for (int loop = 1; loop <= 1000; loop++) {
        int random1 = new Random().nextInt(100);
        int random2 = new Random().nextInt(100);
        if (random1 == random2) {
            System.out.println("Duplicate: " + random1);
        }//from  www  .  ja  v  a 2s .  c o m
    }
}

From source file:Main.java

public static void main(String[] args) {
    Random r = new Random();

    // generate some random boolean values
    boolean[] booleans = new boolean[10];
    for (int i = 0; i < booleans.length; i++) {
        booleans[i] = r.nextBoolean();/*w w  w.  ja  v a  2  s.c  o  m*/
    }

    for (boolean b : booleans) {
        System.out.print(b + ", ");
    }

    // generate a uniformly distributed int random numbers
    int[] integers = new int[10];
    for (int i = 0; i < integers.length; i++) {
        integers[i] = r.nextInt();
    }

    for (int i : integers) {
        System.out.print(i + ", ");
    }

    // generate a uniformly distributed float random numbers
    float[] floats = new float[10];
    for (int i = 0; i < floats.length; i++) {
        floats[i] = r.nextFloat();
    }

    for (float f : floats) {
        System.out.print(f + ", ");
    }

    // generate a Gaussian normally distributed random numbers
    double[] gaussians = new double[10];
    for (int i = 0; i < gaussians.length; i++) {
        gaussians[i] = r.nextGaussian();
    }

    for (double d : gaussians) {
        System.out.print(d + ", ");
    }
}

From source file:Main.java

public static void main(String args[]) {
    int rnd;/*w  w w. j  a v a 2 s .  co m*/
    Random rand = new Random();
    int[] nums = new int[50];

    boolean[] check = new boolean[50];

    for (int k = 0; k < 50; k++) {
        rnd = rand.nextInt(50);
        //check if the check array index has been set
        //if set regenerate since it is duplicate 
        while (check[rnd]) {
            rnd = rand.nextInt(50);
        }
        nums[k] = rnd;
        check[rnd] = true;
    }
    System.out.println(Arrays.toString(nums));

}

From source file:Main.java

public static void main(String[] args) {
    int[][] array = new int[2][10];
    Random rand = new Random();
    for (int i = 0; i < 10; i++) {
        array[0][i] = i;/*  w  w w.  j av a2  s  .co  m*/
        array[1][i] = (int) rand.nextInt(3) + 1;
    }

    System.out.println(Arrays.deepToString(array));

}

From source file:Main.java

public static void main(String... args) {
    int[] data = new int[] { 1, 2, 2, 2, 3, 3, 4, 5, 5, 5, 5, 5, 5 };
    System.out.print(data[new Random().nextInt(data.length)]);
}