class RandomGenerator{ constructor( cache ){ this.rng = new Uint8Array( cache ); this.index = cache-- ; this.generate(); } generate(){ window.crypto.getRandomValues(this.rng); this.index = this.rng.length - 2; return this.rng.length - 1; } next(){ return (this.index < 0)? this.generate() : this.index--; } rand(){ return this.rng[ this.next() ]/256 ; } get( max, min ){ min = ( typeof min === "number")? min : 0 ; return Math.round(this.rand() * (max - min) + min); } roll10( explode ){ explode = ( typeof explode === "number")? differenceToArray( explode, 10 ) : [10]; let dice = this.get( 10, 1 ); return ( explode.indexOf( dice ) >= 0 )? 10 + this.roll10( explode ) : dice ; } rk10( r, k, malus, explode ){ let rolls = []; if((typeof r === "unsigned") || (typeof k === "unsigned")) return 0; malus = ( typeof malus === "unsigned")? 0 : malus ; for(let i = 0 ; i < r ; i++) rolls[i] = this.roll10( explode ); sort( rolls ); return rolls.slice( 0, k ).reduce((a, b) => a + b) - (malus || 0); } } function differenceToArray( min, max ) { let result = [] for( let i = min; i <= max; i++) result.push(i) ; return result; } function sort( array ){ let temp = 0; var unsorted = true ; while(unsorted){ unsorted = false; for(let i = 0; i < array.length -1 ; i++){ if(array[ i ] < array[ i + 1 ]){ temp = array[ i ]; array[ i ] = array[ i + 1 ]; array[ i + 1 ] = temp; unsorted = true; } } } } function sortAssociative( array_arg ){ let result = {}; let array = array_arg; let higher = 1; while( !higher ){ higher = 0; for(let i in array){ higher = (higher)? (array[higher] < array[i])? i : higher : i ; if(array[ i ] < array[ i + 1 ]){ temp = array[ i ]; array[ i ] = array[ i + 1 ]; array[ i + 1 ] = temp; unsorted = true; } } if(higher){ result.push(higher); delete array[ higher ]; higher = 0; } } return result; } function random( max, min ){ min = (typeof min == "undefined")? 0 : min; return Math.floor(Math.random() * (max - min + 1) + min); } function r_gender(){ return (random(1))? "f": "m"; } function arrayAt( index, array ){ let i = 0; let result = {}; for(let obj in array){ if( index == ++i ){ result[obj] = array[obj]; return result; } } } function arrayAtWeight( index, array ){ let i = 0; for(let obj in array){ if(( i <= index ) && ( index <= (i + array[obj].occurence))){ let rs = {}; rs[ obj ] = array[obj]; return rs; } i += array[obj].occurence ; } return null; } function arrayLength( array ){ let i = 0; for(let obj in array){ i++; }; return i; } function arrayWeight( array ){ let i = 0; for(let obj in array){ i += array[obj].occurence; }; return i; } function randomFromAssociative( array ){ return arrayAt( random( arrayLength( array ), 1), array) ; } function randomFromArray( array ){ return array[ random( arrayLength( array )) ] ; } function randomFromWeight( array ){ return arrayAtWeight( random( arrayWeight( array )), array) ; }