fileoverview import _ from "lodash" ; import { GuidUtils } from "./guidUtils" ; import { calculateHash } from "./hashCalculator" ; /**
 * Random number generator that creates a deterministic sequence of random numbers based on an initial seed GUID
 *
 * Warning: This is a very straight forward implementation based on the hashCombine4xUint32 function. It probably
 *          doesn't produce very high quality random numbers (do not use this for cryptography!) and it is not very
 *          efficient.
 */ export class DeterministicRandomGenerator { _guid1 : Uint32Array ; _guid2 : Uint32Array ; _result : Uint32Array ; param in_seed constructor ( in_seed ) { if ( _ . isString ( in_seed ) ) { this . _guid1 = GuidUtils . guidToUint32x4 ( in_seed ) ; } else { this . _guid1 = GuidUtils . guidToUint32x4 ( calculateHash ( String ( in_seed ) ) ) ; } this . _guid2 = new Uint32Array ( 4 ) ; this . _guid2 [ 0 ] = ( this . _guid1 [ 0 ] + 1 ) >>> 0 ; this . _guid2 [ 1 ] = ( this . _guid1 [ 1 ] + 1 ) >>> 0 ; this . _guid2 [ 2 ] = ( this . _guid1 [ 2 ] + 1 ) >>> 0 ; this . _guid2 [ 3 ] = ( this . _guid1 [ 3 ] + 1 ) >>> 0 ; this . _result = new Uint32Array ( 4 ) ; } param in_max returns random ( in_max = 1.0 ) { const randomInteger = this . irandom ( ) ; return randomInteger / 4294967296 * in_max ; } param in_max returns irandom ( in_max ? ) { GuidUtils . hashCombine4xUint32 ( this . _guid1 , this . _guid2 , this . _result ) ; for ( let i = 0 ; i < 4 ; i ++ ) { this . _guid1 [ i ] = this . _guid2 [ i ] ; this . _guid2 [ i ] = this . _result [ i ] ; } if ( in_max === undefined ) { return this . _guid1 [ 0 ] ; } else { if ( in_max < 16777619 ) { return ( ( this . _guid1 [ 0 ] ) % 16777619 ) % in_max ; } else { return this . _guid1 [ 0 ] % in_max ; } } } }