/*! \brief Random helpers. \see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random \see https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range */ export function generateN(count: number, generatefn: (...args: U[]) => R, ...args: U[]) { const ret = new Array(count); for (let i = 0; i < count; i++) ret[i] = generatefn(...args); return ret; } //! \see https://stackoverflow.com/a/2117523 export function uuidv4() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { const r = Math.random() * 16 | 0; const v = c === 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); } export { uuidv4 as uuid }; export function randomIntInclusive(min: number, max: number) { return Math.floor(Math.random() * (max - min + 1)) + min; } export function randomIntExclusive(min: number, max: number) { return Math.floor(Math.random() * (max - min)) + min; } //! \see https://github.com/lodash/lodash/blob/4.17.10/lodash.js#L14077 export function randomFloatInclusive(min: number, max: number) { const rand = Math.random(); return Math.min(min + (rand * (max - min + parseFloat('1e-' + ((rand + '').length - 1)))), max); } export function randomFloatExclusive(min: number, max: number) { return Math.random() * (max - min) + min; } export function randomChar(alphabet: string) { return alphabet[randomIntExclusive(0, alphabet.length)] } export function randomString(size: number, alphabet: string) { return generateN(size, randomChar, alphabet).join(''); }