Skip to content

Instantly share code, notes, and snippets.

@krohne
Last active November 7, 2017 20:40
Show Gist options
  • Save krohne/0d71575dcd2c2a525de890f9efc6e0e6 to your computer and use it in GitHub Desktop.
Save krohne/0d71575dcd2c2a525de890f9efc6e0e6 to your computer and use it in GitHub Desktop.
random decimal digits using Node crypto
// Limited to max 16 digits
function randomDigits(digits) {
return parseInt(
crypto
.randomBytes(Math.floor(digits/2))
.toString('hex')
,16)
.toString(10)
.substr(-digits);
}
console.log(randomDigits(12));
console.log(randomDigits(16));
console.log(randomDigits(17)); // scientific notation string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment