RandomDemo.java


Below is the syntax highlighted version of RandomDemo.java from §9.8 Data Analysis.



/******************************************************************************
 *  Compilation:  javac RandomDemo.java
 *  Execution:    java RandomDemo
 * 
 *  % java RandomDemo
 *  false
 *  -1571432423
 *  51
 *  0.44762832574617084
 *  -0.11571220872310763
 * 
 *  % java RandomDemo
 *  false
 *  -215643072
 *  15
 *  0.7456763003263982
 *  0.3196249757515685
 *
 ******************************************************************************/

import java.util.Random;

public class RandomDemo {

    public static void main(String[] args) { 
        // Random random = new Random(1234567L);
        Random random = new Random();
        boolean a = random.nextBoolean();   // true or false
        int     b = random.nextInt();       // between -2^31 and 2^31 - 1
        int     c = random.nextInt(100);    // between 0 and 99
        double  d = random.nextDouble();    // between 0.0 and 1.0
        double  e = random.nextGaussian();  // gaussian with mean 0 and stddev = 1
        StdOut.println(a);
        StdOut.println(b);
        StdOut.println(c);
        StdOut.println(d);
        StdOut.println(e);
    }

}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.