Skip to content

Instantly share code, notes, and snippets.

@akx
Created September 12, 2017 13:17
Show Gist options
  • Save akx/7afb144ed2b4d32886a29bfd45e7986f to your computer and use it in GitHub Desktop.
Save akx/7afb144ed2b4d32886a29bfd45e7986f to your computer and use it in GitHub Desktop.
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