Skip to content

Instantly share code, notes, and snippets.

@VityaSchel
Last active April 9, 2023 16:09
Show Gist options
  • Save VityaSchel/9e73ed371371d1c3baae4de0a33b22d7 to your computer and use it in GitHub Desktop.
Save VityaSchel/9e73ed371371d1c3baae4de0a33b22d7 to your computer and use it in GitHub Desktop.
JS Random range visualizer
/* RANGE VISUALIZER BY VITYASCHEL
This script is useful when you want to check equality of random results.
Run testingRandom with function and limit of checks
More checks = more accurate
Example: testingRandom(() => Math.random().toFixed(1), 10000)
Output:
0.0 *********
0.1 *******************
0.2 *******************
0.3 ******************
0.4 ********************
0.5 *******************
0.6 *******************
0.7 ******************
0.8 *******************
0.9 *******************
1.0 *********
*/
function testingRandom(func, limitChecks = 10000) {
let objSrc = {}
for (let i = 0; i < limitChecks; i++){
let res = func()
let count = objSrc[res]
if(count === undefined)
count = 0
objSrc[res] = count+1
}
let ordered = Object.keys(objSrc).sort().reduce(
(obj, key) => {
obj[key] = objSrc[key];
return obj
},
{}
)
let visual = ""
let maxOfValues = Math.max(...Object.values(ordered))
let stringOfSymbols = '********************'
let maxSymbols = stringOfSymbols.length
for(let [k, v] of Object.entries(ordered)) {
let timesOfSymbol = (v*maxSymbols)/maxOfValues
visual+=k+' '+stringOfSymbols.substring(0,timesOfSymbol)+' ('+(v/limitChecks*100)+'%)\n'
}
console.log(visual)
return ordered
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment