-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
33 lines (27 loc) · 1.03 KB
/
functions.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
// returns a random integer between min (included) and max (included)
function randInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// returns a random float between min (included) and max (included)
function randFloat(min, max) {
return Math.random() * (max - 1 - min + 1) + min;
}
// returns a random item from the list of items passed in
function randItem(items) {
return items[randInt(0, items.length - 1)];
}
// returns the number passed in with a precision number of digits after the decimal point
function toPrecision(number, precision) {
return +((number).toFixed(precision));
}
// shuffles items and returns a new array. Based on the modern Fisher-Yates shuffle algorithm
function shuffleItems(items) {
return items.reduce((result, item, index) => {
if (index === items.length - 1) {
return result;
}
const jindex = Math.floor(Math.random() * (items.length - 1 - index + 1)) + index;
[result[index], result[jindex]] = [result[jindex], result[index]];
return result;
}, items);
}