graphene
cli graphs
Installation
npm i @jrvieira/graphene
Usage
graphene produces a cli graph based on a collection of samples from a function that returns a number
/* sample.js */
const graphene = require('@jrvieira/graphene')
// grab command line argument as the number of samples
let n = Number(process.argv[2]) || undefined
// return a pseudo-random number
function boxmuller () {
let u = Math.random()
let v = Math.random()
return Math.floor(10 * Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v))
}
// using the Box-Muller transform, a normal distribution is expected
graphene(boxmuller, n)
Running:
node sample 1000000 -amp
Prints similar to:
-50 | 2
-49 | 1
-48
-47
-46 | 1
-45 | 1
-44 | 1
-43 | 7
-42 | 5
-41 | 16
-40 | 16
-39 | 19
-38 | 33
-37 | 51
-36 | 71
-35 | 114
-34 | 158
-33 | 206
-32 | 287
-31 | 395
-30 || 512
-29 || 678
-28 ||| 950
-27 ||| 1193
-26 |||| 1575
-25 ||||| 1993
-24 ||||||| 2525
-23 |||||||| 3263
-22 |||||||||| 3961
-21 |||||||||||| 4759
-20 ||||||||||||||| 6012
-19 |||||||||||||||||| 7304
-18 |||||||||||||||||||||| 8854
-17 ||||||||||||||||||||||||| 10092
-16 ||||||||||||||||||||||||||||| 12037
-15 |||||||||||||||||||||||||||||||||| 13908
-14 ||||||||||||||||||||||||||||||||||||||| 15973
-13 |||||||||||||||||||||||||||||||||||||||||||| 18388
-12 |||||||||||||||||||||||||||||||||||||||||||||||||| 20543
-11 ||||||||||||||||||||||||||||||||||||||||||||||||||||||| 22934
-10 ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 25360
-9 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 27927
-8 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 30147
-7 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 32166
-6 ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 34118
-5 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 36001
-4 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 37277
-3 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 38451
-2 ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 39582
-1 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 39788
0 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 39757
1 ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 39562
2 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 38721
3 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 37680
4 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 35702
5 ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 34543
6 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 32150
7 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 30118
8 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 27764
9 ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 25420
10 |||||||||||||||||||||||||||||||||||||||||||||||||||||||| 23039
11 |||||||||||||||||||||||||||||||||||||||||||||||||| 20626
12 |||||||||||||||||||||||||||||||||||||||||||| 18329
13 ||||||||||||||||||||||||||||||||||||||| 16160
14 |||||||||||||||||||||||||||||||||| 14056
15 ||||||||||||||||||||||||||||| 11876
16 ||||||||||||||||||||||||| 10344
17 ||||||||||||||||||||| 8604
18 |||||||||||||||||| 7282
19 ||||||||||||||| 6050
20 |||||||||||| 4794
21 |||||||||| 3954
22 |||||||| 3171
23 |||||| 2445
24 ||||| 1974
25 |||| 1580
26 ||| 1195
27 ||| 921
28 || 668
29 || 502
30 | 373
31 | 288
32 | 213
33 | 141
34 | 133
35 | 59
36 | 52
37 | 28
38 | 27
39 | 16
40 | 9
41 | 10
42 | 3
43 | 2
44 | 1
45
46 | 1
47
48 | 1
Arguments
graphene(f, s, r)
- f : Function
- Function to run for each sample.
- s : Number (optional)
- Number of samples to take. Defaults to 100.
- r : Number (optional)
- Output line limit.
Flags (cli)
- -amp
- Stretch the graph so that the max value occupies the full width of the current terminal. By default, bars would stay proportional to the total number of samples.
- -1
- Limit processor and memory usage to the node standard. Currently, 1 core and 512mb on 32-bit systems or 1gb on 64-bit systems.
- -top
- Limit output to x top values, where x is the number of lines available at the current terminal.
More examples
Check the examples folder for a positive-skew distribution of a collector's probability of completing a set at random and a crude brute force attempt at the 25 Horses Puzzle:
node examples/collect -amp
node examples/25hp