Last active
November 7, 2017 20:40
-
-
Save krohne/0d71575dcd2c2a525de890f9efc6e0e6 to your computer and use it in GitHub Desktop.
random decimal digits using Node crypto
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
// 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