Created
September 12, 2017 13:17
-
-
Save akx/7afb144ed2b4d32886a29bfd45e7986f to your computer and use it in GitHub Desktop.
This file contains 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 crypto = require("crypto"); | |
function gen( | |
rand = Math.random, | |
n = 10, | |
alphabet = "abcdefghijklmnopqrstuvwxyz" | |
) { | |
let p = ""; | |
for (var i = 0; i < n; i++) | |
p += alphabet[Math.floor(rand() * alphabet.length)]; | |
return p; | |
} | |
function printCharCounts(d) { | |
const counter = {}; | |
for (var i = 0; i < d.length; i++) { | |
counter[d[i]] = (counter[d[i]] || 0) + 1; | |
} | |
Object.keys(counter) | |
.sort() | |
.forEach(key => { | |
console.log(`${key}\t${counter[key]}`); | |
}); | |
} | |
let randomBuf = []; | |
const cryptoBufferRand = () => { | |
if (!randomBuf.length) { | |
randomBuf = randomBuf.concat(Array.from(crypto.randomBytes(512))); | |
} | |
return randomBuf.shift() / 255; | |
}; | |
const cryptoRand = () => crypto.randomBytes(1)[0] / 255; | |
printCharCounts(gen(Math.random, 100000)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment