I am trying to generate exactly 6
random digits in Node.js, which needs to be cryptographically secure. Here is my code:
var crypto = require('crypto');
crypto.randomBytes(2, function(err, buffer) {
console.log(parseInt(buffer.toString('hex'), 16));
});
The problem is that the result from this can be 4
or 5
digits, because we are converting from hex to decimal. Is there a way to keep the cryptographically secure function randomBytes()
, but guarantee a 6 digit result?