-
Notifications
You must be signed in to change notification settings - Fork 0
/
bellCurve.js
73 lines (62 loc) · 1.72 KB
/
bellCurve.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const values = {}
// max Count to run
let maxCount = 1000000
//
// function to deal with randomness
const randomNumber = () => Math.random()
//
// returns a function to create a random number on a dynamically assigned range
let modifierGenerator = (range) => {
return () => randomNumber() * range
}
// holds all modifier functions
let modifierArr = []
// total range of the added modifier ranges
let totalRange = 0
// amount of modifier to create
let amountOfModifer = Math.ceil(randomNumber() * 8 + 2)
// generate all modifier functions
for (let i = 0; i < amountOfModifer; i++) {
let num = Math.ceil(randomNumber() * 15)
totalRange += num
modifierArr.push(modifierGenerator(num))
console.log(`range for generator ${i}: ${num}`)
}
// loading values with all needed keys
for (let i = 0; i < totalRange + 1; i++) {
values[i] = 0
}
// generate a number based on all modifiers
// log that values in values object
function logModifier() {
let num = 0
modifierArr.every(elem => num += elem())
num = Math.ceil(num)
values[num]++
}
let count = 0
// iterate up to maxCount times
while (count < maxCount) {
logModifier()
count++
}
console.log("values:", values)
// recursive function to generate many *
function convertToDot(num) {
if (num == 0) return ""
return convertToDot(num - 1) + "*"
}
// Find max amount
let maxVal = 0
for (prop in values) {
if (values[prop] > maxVal) maxVal = values[prop]
}
// create increment for max amount of * to display
let width = process.stdout.columns - 10
let increment = Math.floor(maxVal / width)
// logging our bell curve
for (prop in values) {
if (values[prop] < 0) continue
values[prop] = Math.floor(values[prop] / increment)
console.log(`${prop}: ${prop < 10 ? " " : ""}${convertToDot(values[prop])}`)
}