UNPKG

rndlib

Version:

Easy to use randomizer functions with TypeScript support

346 lines (328 loc) 10.8 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.rndlib = {})); })(this, (function (exports) { 'use strict'; /** * Return a random integer number between two provided values. If only * one value is provided, it is considered to be the max value, min is 0. * * @param min number * @param max number */ function rnd(min, max) { var rMin = max !== undefined ? min : 0; var rMax = max !== undefined ? max : min; if (rMin > rMax) throw new Error("Invalid values: Minimum boundary on rnd() cannot be higher than maximum boundary: " + rMin + " - " + rMax); if (rMin === rMax) return rMin; return Math.floor(Math.random() * (rMax + 1 - rMin)) + rMin; } /** * Return a random value from the provided array * * @param arr array<T> */ function arnd(arr) { return arr[Math.floor(Math.random() * arr.length)]; } /** * Returns a value based on gaussi * @param mid * @param steps * @param depth * @param minCut * @param maxCut * @returns */ function grnd(mid, steps, depth, minCut, maxCut) { function insideCut(val) { if (typeof minCut === "number" && val < minCut) return false; if (typeof maxCut === "number" && val > maxCut) return false; return true; } var res = mid; var maxDepth = depth * 2; // const mods: Array<number | string> = []; for (var i = 0; i < steps; i++) { var mod = rnd(0, maxDepth) - depth; // mods.push(mod); if (insideCut(res + mod)) { res += mod; } } // if (typeof minCut === "number" && res < minCut) console.log(mods); return res; } /** * Return a random number between min and max values, but distribution of the values resembles normal distribution. * * This does not follow proper mathematical normalized deviation, but is an approximation. * * @param min number Bottom limit of the normalized rand * @param max number Top limit of the normalized rand * @param ramp 0-10 Higher the number, then more aggressive the curve * @returns */ function nrnd(min, max, ramp) { if (min > max) throw new Error("Invalid value: Minimum cannot be higher than max value. " + min + " - " + max); var aggro = ramp !== undefined ? ramp : 5; if (aggro < 0 || aggro > 10) throw new Error("Invalid ramp value " + aggro + ": Must be between 0 and 10"); if (aggro === 0) { return rnd(min, max); } var mid = Math.round(min + (max - min) / 2); var steps = 11 - aggro; var depthPerc = 1 - aggro / 10; var depth = Math.round((mid * depthPerc) / 2); return grnd(mid, steps, depth, min, max); } /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /** @deprecated */ function __spreadArrays() { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; } /** * Return a shallow copy the provided array and shuffle its contents. * * @param Array<T> * @returns Array<T> */ function shuffle(arr) { // const maxLen = arr.length; var copyArr = __spreadArrays(arr); var newArr = []; while (copyArr.length > 0) { var ind = rnd(0, copyArr.length - 1); newArr.push(copyArr.splice(ind, 1)[0]); } return newArr; } /** * Returns true on a percentage chance provided. * * @param percentage number * @returns */ function chance(percentage) { if (percentage < 0 || percentage > 100) throw new Error("Invalid value " + percentage + ". Valid range is between 0-100"); var d100Roll = Math.floor(Math.random() * 100); if (percentage > d100Roll) { return true; } return false; } /** * Return the number of results that are equal or higher than threshold * @param threshold * @param pool */ function successPool(threshold, pool) { var results = Array.isArray(pool) ? pool : pool.rolls; return results.reduce(function (sum, res) { if (res >= threshold) return sum + 1; return sum; }, 0); } /** * Return the number of results that are equal or higher than threshold * @param threshold * @param pool */ function sumPool(pool) { var results = Array.isArray(pool) ? pool : pool.rolls; return results.reduce(function (sum, res) { return sum + res; }, 0); } /** * Get results for string die roll like 2d10+3 * * @param dieString * @returns */ function roll(dieString) { var diere = /(\d*)d(\d+)([\+\-]*)(\d*)/im; var die = dieString.replace(" ", "").match(diere); var times = Number(die[1] || 1); var sides = Number(die[2]); var bnum = Number(die[4] || 0); var bonus = die[3] === "-" ? bnum * -1 : bnum * 1; var res = dx(sides, times); var sum = sumPool(res) + bonus; var results = { sum: sum, rolls: res, dieType: sides }; return results; } /** * Internal dice roller for any side * * @param sides * @param times * @returns */ function dx(sides, times) { var repeat = times === undefined ? 1 : times; var pool = []; for (var i = 0; i < repeat; i++) { pool.push(rnd(1, sides)); } return pool; } /** * Roll d4 n Times and return the results of each roll * * @param times How many 4 sided die are rolled. * @returns */ function d4(times) { return dx(4, times); } /** * Roll d6 n Times and return the results of each roll * * @param times How many 6 sided die are rolled. * @returns */ function d6(times) { return dx(6, times); } /** * Roll d8 n Times and return the results of each roll * * @param times How many 4 sided die are rolled. * @returns */ function d8(times) { return dx(8, times); } /** * Roll d10 n Times and return the results of each roll * * @param times How many 4 sided die are rolled. * @returns */ function d10(times) { return dx(10, times); } /** * Roll d12 n Times and return the results of each roll * * @param times How many 4 sided die are rolled. * @returns */ function d12(times) { return dx(12, times); } /** * Roll d20 n Times and return the results of each roll * * @param times How many 4 sided die are rolled. * @returns */ function d20(times) { return dx(20, times); } /** * Roll d100 n Times and return the results of each roll * * @param times How many 4 sided die are rolled. * @returns */ function d100(times) { return dx(100, times); } /** * Return a random integer number as a string with leading zeroes equal to the length of max. * * If the random values is requested between 1 and 999, following results would be valid: * * 13 => "013" * 123 => "123" * * @param min number * @param max number */ function padrnd(min, max) { var num = rnd(min, max); var maxLen = max !== undefined ? max.toString().length : min.toString().length; var numStr = num.toString(); return numStr.padStart(maxLen, "0"); } /** * Return a random values from the provided array * * @param arr array<T> * @param amout number Amount of values to return * @param unique boolean Provided values must be unique */ function arnds(arr, amount, unique) { if (unique === void 0) { unique = false; } if (unique && arr.length <= amount) { return __spreadArrays(arr); } var res = []; if (!unique) { for (var i = 0; i < amount; i++) { if (!unique) { res.push(arnd(arr)); } } } else { while (res.length < amount) { var val = arnd(arr); if (!res.includes(val)) { res.push(val); } } } return res; } var dice = { d4: d4, d6: d6, d8: d8, d10: d10, d12: d12, d20: d20, d100: d100, roll: roll }; exports.Dice = dice; exports.arnd = arnd; exports.arnds = arnds; exports.chance = chance; exports.nrnd = nrnd; exports.padrnd = padrnd; exports.rnd = rnd; exports.shuffle = shuffle; exports.successPool = successPool; exports.sumPool = sumPool; Object.defineProperty(exports, '__esModule', { value: true }); }));