@studiometa/js-toolkit
Version:
A set of useful little bits of JavaScript to boost your project! 🚀
20 lines (19 loc) • 496 B
JavaScript
import { isArray, isString } from "./is.js";
function random(a, b = 0) {
return Math.random() * (b - a) + a;
}
function randomInt(a, b = 0) {
return Math.round(random(a, b));
}
function randomItem(items) {
if (!isArray(items) && !isString(items)) {
throw new Error("randomItem() expects an array or a string as argument.");
}
return items.length > 0 ? items[randomInt(items.length - 1)] : void 0;
}
export {
random,
randomInt,
randomItem
};
//# sourceMappingURL=random.js.map