boxes/js/utils/random.mjs

22 lines
445 B
JavaScript

export function range (min, max) {
return min + Math.random() * (max - min);
}
export function integer (min, max) {
return Math.floor(range(min, max));
}
export function item (array) {
return array[integer(0, array.length)];
}
export function shuffle (array) {
const copy = [ ...array ];
const result = [];
while (copy.length > 0) {
const index = integer(0, copy.length);
result.push(copy.splice(index, 1)[0]);
}
return result;
}