UNPKG

gnablib

Version:

A lean, zero dependency library to provide a useful base for your project.

2 lines 622 B
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */ import{sNum as t}from"../safe/safe.js";export class RandTk{constructor(t){this.rs=t}floatBetween(t,e){return this.rs()*(e-t)-t}intBetween(t,e){return t=Math.ceil(t),e=Math.floor(e),Math.floor(this.rs()*(e-t)+t)}wtRnd(t){const e=this.floatBetween(0,t[t.length-1]),r=t.length-1;for(let n=0;n<r;n++)if(e<t[n])return n;return r}weightedCumulative(e){let r=0;for(let n=0;n<e.length;n++)t(`weights[${n}]`,e[n]).atLeast(r).throwNot(),r=e[n];return this.wtRnd(e)}weightedRelative(t){const e=[];let r=0;for(let n=0;n<t.length;n++)e[n]=t[n]+r,r=e[n];return this.wtRnd(e)}}