UNPKG

@kotori-bot/tools

Version:
87 lines (86 loc) 2.71 kB
/** * @Package @kotori-bot/tools * @Version 1.5.2 * @Author Hotaru <me@hotaru.icu> * @Copyright 2024-2025 Hotaru. All rights reserved. * @License BAN-ZHINESE-USING * @Link https://github.com/kotorijs/kotori * @Date 17:26:22 */ "use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var random_exports = {}; __export(random_exports, { random: () => random }); module.exports = __toCommonJS(random_exports); class Random { seed; constructor(seed = Math.random) { this.seed = seed; } int(min = 0, max = 100) { const minHandle = Math.ceil(min); const maxHandle = Math.floor(max); return Math.floor(this.seed() * (maxHandle - minHandle)) + minHandle; } float(min = 0, max = 1) { return this.seed() * (max - min) + min; } bool(offset = 0.5) { return this.seed() < offset; } choice(list) { if (list.length === 0) { throw new Error("Cannot choose from an empty list"); } const index = this.int(0, list.length); return list[index]; } shuffle(list) { const shuffled = [...list]; for (let i = shuffled.length - 1; i > 0; i--) { const j = this.int(0, i + 1); [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]; } return shuffled; } uuid() { return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => { const r = this.seed() * 16 | 0; const v = c === "x" ? r : r & 3 | 8; return v.toString(16); }); } } var random; ((_random) => { const random2 = new Random(); _random.int = random2.int.bind(random2); _random.float = random2.float.bind(random2); _random.bool = random2.bool.bind(random2); _random.choice = random2.choice.bind(random2); _random.shuffle = random2.shuffle.bind(random2); _random.uuid = random2.uuid.bind(random2); _random.clone = (seed = Math.random) => new Random(seed); })(random || (random = {})); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { random });