$ node test.js
Created
January 27, 2021 02:55
-
-
Save sofish/a2ebd14e4aeff5d61805f82068d4f2e2 to your computer and use it in GitHub Desktop.
random.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const os = require('os'); | |
module.exports = function random() { | |
return +(process.hrtime()[1] * os.freemem() % 1000 / 1000).toFixed(2); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rand = require('./random'); | |
const map = {}; | |
const mapLang = {}; | |
for (let i =0; i < 2000; i++) { | |
incr(Math.floor(rand() * 10), map); | |
incr(Math.floor(Math.random() * 10), mapLang); | |
} | |
console.log(JSON.stringify(map, null, 2)); | |
console.log(JSON.stringify(mapLang, null, 2)); | |
function incr(k, data) { | |
if(data[k]) { | |
data[k] += 1; | |
} else { | |
data[k] = 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment