/**
 * A naive, partial port of C#'s Random class
 */ class CsRandom { static MBIG : number = 2147483647 ; static MSEED : number = 161803398 ; static MZ : number = 0 ; inext : number ; inextp : number ; seedArray : number [ ] = [ ] ; constructor ( seed ) { let ii ; let mj , mk ; let subtraction = ( seed === - 2147483648 ) ? 2147483647 : Math . abs ( seed ) ; mj = CsRandom . MSEED - subtraction ; this . seedArray [ 55 ] = mj ; mk = 1 ; for ( let i = 1 ; i < 55 ; i ++ ) { ii = ( 21 * i ) % 55 ; this . seedArray [ ii ] = mk ; mk = mj - mk ; if ( mk < 0 ) mk += CsRandom . MBIG ; mj = this . seedArray [ ii ] ; } for ( let k = 1 ; k < 5 ; k ++ ) { for ( let i = 1 ; i < 56 ; i ++ ) { this . seedArray [ i ] -= this . seedArray [ 1 + ( i + 30 ) % 55 ] ; if ( this . seedArray [ i ] < 0 ) this . seedArray [ i ] += CsRandom . MBIG ; } } this . inext = 0 ; this . inextp = 21 ; seed = 1 ; } private sample ( ) { return ( this . internalSample ( ) * ( 1.0 / CsRandom . MBIG ) ) ; } private internalSample ( ) { let retVal ; let locINext = this . inext ; let locINextp = this . inextp ; if ( ++ locINext >= 56 ) locINext = 1 ; if ( ++ locINextp >= 56 ) locINextp = 1 ; retVal = this . seedArray [ locINext ] - this . seedArray [ locINextp ] ; if ( retVal === CsRandom . MBIG ) retVal -- ; if ( retVal < 0 ) retVal += CsRandom . MBIG ; this . seedArray [ locINext ] = retVal ; this . inext = locINext ; this . inextp = locINextp ; return retVal ; } next ( ) { return this . internalSample ( ) ; } private getSampleForLargeRange ( ) { let result = this . internalSample ( ) ; let negative = ( this . internalSample ( ) % 2 === 0 ) ? true : false ; if ( negative ) { result = - result ; } let d = result ; d += ( 2147483647 - 1 ) ; d /= 2 * 2147483647 - 1 ; return d ; } nextInRange ( minValue , maxValue ) { if ( minValue > maxValue ) { throw new Error ( "max less than min" ) ; } let range = maxValue - minValue ; if ( range <= 2147483647 ) { return ( Math . floor ( this . sample ( ) * range ) + minValue ) ; } else { return Math . floor ( Math . floor ( this . getSampleForLargeRange ( ) * range ) + minValue ) ; } } nextLessThan ( maxValue ) { if ( maxValue < 0 ) { throw new Error ( "Max value less than 0" ) ; } return Math . floor ( this . sample ( ) * maxValue ) ; } nextDouble ( ) { return this . sample ( ) ; } }