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:MainClass.java

public static void main(String args[]) {
    Random randomNumbers = new Random(); // random number generator

    int frequency1 = 0; // count of 1s rolled
    int frequency2 = 0; // count of 2s rolled
    int frequency3 = 0; // count of 3s rolled
    int frequency4 = 0; // count of 4s rolled
    int frequency5 = 0; // count of 5s rolled
    int frequency6 = 0; // count of 6s rolled

    int face; // stores most recently rolled value

    // summarize results of 6000 rolls of a die
    for (int roll = 1; roll <= 6000; roll++) {
        face = 1 + randomNumbers.nextInt(6); // number from 1 to 6

        // determine roll value 1-6 and increment appropriate counter
        switch (face) {
        case 1:/*from www.j  av  a 2s. c o m*/
            ++frequency1; // increment the 1s counter
            break;
        case 2:
            ++frequency2; // increment the 2s counter
            break;
        case 3:
            ++frequency3; // increment the 3s counter
            break;
        case 4:
            ++frequency4; // increment the 4s counter
            break;
        case 5:
            ++frequency5; // increment the 5s counter
            break;
        case 6:
            ++frequency6; // increment the 6s counter
            break; // optional at end of switch
        }
    }

    System.out.println("Face\tFrequency"); // output headers
    System.out.printf("1\t%d\n2\t%d\n3\t%d\n4\t%d\n5\t%d\n6\t%d\n", frequency1, frequency2, frequency3,
            frequency4, frequency5, frequency6);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    int bitLength = 512; // 512 bits
    Random rnd = new Random();
    System.out.println("BitLength : " + bitLength);
    BigInteger mod = new BigInteger(bitLength, rnd);

}

From source file:Main.java

public static void main(String[] args) {
    DoubleStream i = DoubleStream.generate(new Random()::nextDouble);
    Stream<Double> o = i.boxed();

    o.limit(10).forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {

    int bitLength = 3;

    // create a random object
    Random rnd = new Random();

    BigInteger bi = BigInteger.probablePrime(bitLength, rnd);

    System.out.println(bi);/* w  w  w .  j  a  v a  2s. c o m*/
}

From source file:Main.java

public static void main(String[] args) {
    int[] anArray = new int[1000];
    Random generator = new Random();
    for (int i = 0; i < 1000; i++) {
        anArray[i] = (generator.nextInt(1000) + 1);
    }//from   w  w  w .  j  a v a2s .  c  om
    Date before = new Date();
    Arrays.sort(anArray);
    Date after = new Date();
    System.out.println("milli seconds" + (after.getTime() - before.getTime()));
}

From source file:Main.java

public static void main(String args[]) {
    anArray = new double[10];
    Random rand = new Random();
    for (int i = 0; i < 10; i++) {
        anArray[i] = rand.nextInt();/*from ww  w  .jav  a  2 s . c o  m*/
    }
    System.out.println(Arrays.toString(anArray));
}

From source file:Main.java

public static void main(final String[] args) throws Exception {
    Random random = new Random();
    Set<Integer> intSet = new HashSet<>();
    while (intSet.size() < 6) {
        intSet.add(random.nextInt(49) + 1);
    }//  w  ww.ja va2s . c  o  m
    Integer[] ints = intSet.toArray(new Integer[intSet.size()]);
    System.out.println(Arrays.toString(ints));
}

From source file:Main.java

public static void main(String[] args) {
    Random random = new Random();
    Set<Integer> set = new HashSet<Integer>();
    while (set.size() < 5) {
        set.add(random.nextInt());/*from  w w  w .  ja v  a2  s.  c  o m*/
    }
    List<Integer> result = new ArrayList<Integer>(set);
    System.out.println(result);
}

From source file:Main.java

public static void main(String[] args) {
    char array[][] = new char[5][5];
    Random rnd = new Random();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            int x = rnd.nextInt(5); // 0 to 4
            switch (x) {
            case 0: {
                array[i][j] = 'a';
                break;
            }//from   w w w  . j  ava  2  s .c o m
            case 1: {
                array[i][j] = 'b';
                break;
            }
            case 2: {
                array[i][j] = 'c';
                break;
            }
            case 3: {
                array[i][j] = 'd';
                break;
            }
            case 4: {
                array[i][j] = 'e';
                break;
            }
            }
        }
    }
    System.out.println(Arrays.deepToString(array));
}

From source file:Main.java

public static void main(String[] args) {
    Set<Integer> intSet = new LinkedHashSet<Integer>();
    Random r = new Random();
    while (intSet.size() <= 6) {
        intSet.add(r.nextInt(49));//from w w  w.jav a 2s  .  co  m
    }
    System.out.println(intSet);
}