01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package algs11;
import stdlib.*;
/* ***********************************************************************
* Compilation: javac RandomSeq.java
* Execution: java RandomSeq N lo hi
*
* Prints N numbers between lo and hi.
*
* % java RandomSeq 5 100.0 200.0
* 123.43
* 153.13
* 144.38
* 155.18
* 104.02
*
*************************************************************************/
public class RandomSeq {
public static void main(String[] args) {
args = new String[] { "5", "100.0", "200.0" };
// command-line arguments
int N = Integer.parseInt(args[0]);
if (args.length == 1) {
// generate and print N numbers between 0.0 and 1.0
for (int i = 0; i < N; i++) {
double x = StdRandom.uniform();
StdOut.println(x);
}
}
else if (args.length == 3) {
double lo = Double.parseDouble(args[1]);
double hi = Double.parseDouble(args[2]);
// generate and print N numbers between lo and hi
for (int i = 0; i < N; i++) {
double x = StdRandom.uniform(lo, hi);
StdOut.format("%.2f\n", x);
}
}
else {
throw new IllegalArgumentException("Invalid number of arguments");
}
}
}
|