Skip to content

Instantly share code, notes, and snippets.

@VityaSchel
Last active April 9, 2023 16:09
Show Gist options
  • Select an option

  • Save VityaSchel/9e73ed371371d1c3baae4de0a33b22d7 to your computer and use it in GitHub Desktop.

Select an option

Save VityaSchel/9e73ed371371d1c3baae4de0a33b22d7 to your computer and use it in GitHub Desktop.

Revisions

  1. VityaSchel revised this gist Apr 9, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ function testingRandom(func, limitChecks = 10000) {
    let maxSymbols = stringOfSymbols.length
    for(let [k, v] of Object.entries(ordered)) {
    let timesOfSymbol = (v*maxSymbols)/maxOfValues
    visual+=k+' '+stringOfSymbols.substring(0,timesOfSymbol)+'\n'
    visual+=k+' '+stringOfSymbols.substring(0,timesOfSymbol)+' ('+(v/limitChecks*100)+'%)\n'
    }
    console.log(visual)
    return ordered
  2. VityaSchel created this gist May 6, 2021.
    46 changes: 46 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    /* 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)+'\n'
    }
    console.log(visual)
    return ordered
    }