import { createMWC } from '@src/dexm/mwc' const MAX_TRIALS = 1 const MAX_LOOPS = 100000000 // basic generator settings // const a = 43 const b = 52 const r = 3 // format generator output // to ensure it could be usable as an object hash key // concatenation or array reduction const output = (arr) => { // console.log(arr) //return arr.reduce((acc, val) => b * acc + val, 0) return arr.join(':') } /** * shoot the generator sequence * * @param {Number} a: the generator multiplier * @param {array} seeds: generator seed */ const shoot = (a, seeds) => { const store = {} const generator = createMWC(a, b, r) // iterations let i = 1 // function result let result = 0 // SOME METRICS let newly = 0 let crashes = 0 generator.init(seeds) generator.next() // avoid collision at fist loop ? store[output(seeds)] = 0 console.log('seeds: %s', seeds.join(':')) // shoot test loop while (i++ < MAX_LOOPS) { const generated = generator.next() const val = output(generated) //console.log('generated: %s', generated.join(':')) if(i % 1000000 === 0) console.log(i) if(store.hasOwnProperty(val)) { result = i crashes++ // console.log('#### coolision : %s', i) // break } else { store[val] = i newly++ } // console.log('store[%s]: %s', val, store[val]) // console.log(' %s', generated.join(':')) } // basic metrics display console.log('crashes: %s', crashes) console.log('new values: %s', newly) //console.log(result) return result } // main test const test = () => { let max_loops = 0 let a_max = 0 let seeds = [1, 1, 1, 1] for(let k = 0; k < MAX_TRIALS; k++) { //const a = 4 * Math.floor(52 * Math.random()) - 1 const a = 1717 const shots_count = shoot(a, seeds) console.log('k: %s a: %s', k, a) // console.log('a: %s', a) // console.log('shots_count=%s', shots_count) if(shots_count > max_loops) { max_loops = shots_count a_max = a } } console.log('%s loops %s times....', a_max, max_loops) } console.log('TEST') test() console.log('END.')