-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom-seq.client.js
42 lines (38 loc) · 1.04 KB
/
random-seq.client.js
1
2
3
4
5
6
7
8
9
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
const { StdRandom, StdOut } = require('../../libs')
/**
* RandomSeq
* @classdesc Sample StdOut client
* @see p. 37
* @see {@link https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/RandomSeq.java.html}
*/
class RandomSeq {
/**
* Prints `n` random real numbers in range [lo, hi).
* @param {[]} args [n, lo, hi]
* @example <caption>Generating 5 random doubles between 100-199.99</caption>
* ```sh
* $ node random-seq.client.js 5 100.0 199.99
* 166.29
* 171.09
* 199.85
* 159.78
* 165.14
* ```
* @example <caption>Streaming the StdOut to a file</caption>
* ```sh
* $ node random-seq.client.js 1000 100.0 199.99 > data.txt
* ```
*/
static main (args) {
const n = parseInt(args[0], 10)
const lo = parseFloat(args[1])
const hi = parseFloat(args[2])
for (let i = 0; i < n; i++) {
const x = StdRandom.uniform(lo, hi)
StdOut.printf('%f\n', x.toFixed(2))
}
}
}
// Execution
// ==============================
if (require.main === module) RandomSeq.main(process.argv.slice(2))