function mul64_ ( out , aHi , aLo , bHi , bLo ) { var c1 = ( ( aLo >>> 16 ) * ( bLo & 0xffff ) ) >>> 0 ; var c0 = ( ( aLo & 0xffff ) * ( bLo >>> 16 ) ) >>> 0 ; var lo = ( ( aLo & 0xffff ) * ( bLo & 0xffff ) ) >>> 0 ; var hi = ( ( aLo >>> 16 ) * ( bLo >>> 16 ) + ( ( c0 >>> 16 ) + ( c1 >>> 16 ) ) ) >>> 0 ; c0 = ( c0 << 16 ) >>> 0 ; lo = ( lo + c0 ) >>> 0 ; if ( lo >>> 0 < c0 >>> 0 ) { hi = ( hi + 1 ) >>> 0 ; } c1 = ( c1 << 16 ) >>> 0 ; lo = ( lo + c1 ) >>> 0 ; if ( lo >>> 0 < c1 >>> 0 ) { hi = ( hi + 1 ) >>> 0 ; } hi = ( hi + Math . imul ( aLo , bHi ) ) >>> 0 ; hi = ( hi + Math . imul ( aHi , bLo ) ) >>> 0 ; out [ 0 ] = hi ; out [ 1 ] = lo ; } function add64_ ( out , aHi , aLo , bHi , bLo ) { var hi = ( aHi + bHi ) >>> 0 ; var lo = ( aLo + bLo ) >>> 0 ; if ( lo >>> 0 < aLo >>> 0 ) { hi = ( hi + 1 ) | 0 ; } out [ 0 ] = hi ; out [ 1 ] = lo ; } const defaultIncHi = 0x14057b7e ; const defaultIncLo = 0xf767814f ; const MUL_HI = 0x5851f42d >>> 0 ; const MUL_LO = 0x4c957f2d >>> 0 ; const BIT_53 = 9007199254740992.0 ; const BIT_27 = 134217728.0 ; export class RNG { private state : Int32Array ; constructor ( seedHi ? , seedLo ? , incHi = defaultIncHi , incLo = defaultIncLo ) { let hi ; let lo ; if ( seedLo === undefined && seedHi === undefined ) { lo = ( Math . random ( ) * 0xffffffff ) >>> 0 ; hi = 0 ; } else if ( seedLo === undefined ) { lo = seedHi as number ; hi = 0 ; } else { lo = seedLo ; hi = seedHi as number ; } this . state = new Int32Array ( [ 0 , 0 , incHi >>> 0 , ( incLo | 1 ) >>> 0 ] ) ; this . next ( ) ; add64_ ( this . state , this . state [ 0 ] , this . state [ 1 ] , hi >>> 0 , lo >>> 0 ) ; this . next ( ) ; } getState ( ) { return [ this . state [ 0 ] , this . state [ 1 ] , this . state [ 2 ] , this . state [ 3 ] ] ; } setState ( state ) { this . state [ 0 ] = state [ 0 ] ; this . state [ 1 ] = state [ 1 ] ; this . state [ 2 ] = state [ 2 ] ; this . state [ 3 ] = state [ 3 ] | 1 ; } next ( ) { var oldHi = this . state [ 0 ] >>> 0 ; var oldLo = this . state [ 1 ] >>> 0 ; mul64_ ( this . state , oldHi , oldLo , MUL_HI , MUL_LO ) ; add64_ ( this . state , this . state [ 0 ] , this . state [ 1 ] , this . state [ 2 ] , this . state [ 3 ] ) ; var xsHi = oldHi >>> 18 ; var xsLo = ( ( oldLo >>> 18 ) | ( oldHi << 14 ) ) >>> 0 ; xsHi = ( xsHi ^ oldHi ) >>> 0 ; xsLo = ( xsLo ^ oldLo ) >>> 0 ; var xorshifted = ( ( xsLo >>> 27 ) | ( xsHi << 5 ) ) >>> 0 ; var rot = oldHi >>> 27 ; var rot2 = ( ( - rot >>> 0 ) & 31 ) >>> 0 ; return ( ( xorshifted >>> rot ) | ( xorshifted << rot2 ) ) >>> 0 ; } integer ( max ) { if ( ! max ) { return this . next ( ) ; } max = max >>> 0 ; if ( ( max & ( max - 1 ) ) === 0 ) { return this . next ( ) & ( max - 1 ) ; } var num = 0 ; var skew = ( - max >>> 0 ) % max >>> 0 ; for ( num = this . next ( ) ; num < skew ; num = this . next ( ) ) { } return num % max ; } number ( ) { var hi = ( this . next ( ) & 0x03ffffff ) * 1.0 ; var lo = ( this . next ( ) & 0x07ffffff ) * 1.0 ; return ( hi * BIT_27 + lo ) / BIT_53 ; } }