001package algs11;
002import stdlib.*;
003/* ***********************************************************************
004 *  Compilation:  javac RandomSeq.java
005 *  Execution:    java RandomSeq N lo hi
006 *
007 *  Prints N numbers between lo and hi.
008 *
009 *  % java RandomSeq 5 100.0 200.0
010 *  123.43
011 *  153.13
012 *  144.38
013 *  155.18
014 *  104.02
015 *
016 *************************************************************************/
017
018public class RandomSeq {
019        public static void main(String[] args) {
020                args = new String[] { "5", "100.0", "200.0" };
021
022                // command-line arguments
023                int N = Integer.parseInt(args[0]);
024
025                if (args.length == 1) {
026                        // generate and print N numbers between 0.0 and 1.0
027                        for (int i = 0; i < N; i++) {
028                                double x = StdRandom.uniform();
029                                StdOut.println(x);
030                        }
031                }
032
033                else if (args.length == 3) {
034                        double lo = Double.parseDouble(args[1]);
035                        double hi = Double.parseDouble(args[2]);
036
037                        // generate and print N numbers between lo and hi
038                        for (int i = 0; i < N; i++) {
039                                double x = StdRandom.uniform(lo, hi);
040                                StdOut.format("%.2f\n", x);
041                        }
042                }
043
044                else {
045                        throw new IllegalArgumentException("Invalid number of arguments");
046                }
047        }
048}