Skip to content

Instantly share code, notes, and snippets.

@aryomuzakki
Last active September 14, 2023 12:17
Show Gist options
  • Save aryomuzakki/b0fa350abcfa93598c8ce57883933d38 to your computer and use it in GitHub Desktop.
Save aryomuzakki/b0fa350abcfa93598c8ce57883933d38 to your computer and use it in GitHub Desktop.
Generate Random Number and String in Javascript, using Math.random(), Node JS Crypto (server side), or Web Crypto API (client side / browser)
// nodejs crypto (server side)
// Math.random() replacement
const cryptoRandom = () => parseInt(require("crypto").randomBytes(4).toString("hex"), 16) / (0xffffffff + 1)
// generate random number integer based on minimum and maximum
const cryptoRandomNumber = (min, max) => require("crypto").randomInt(min, max + 1)
// generate random string (hex/base64) based on bytes
const cryptoRandomString = (bytes, encoding = "hex") => require("crypto").randomBytes(bytes).toString(encoding)
// generate random string (hex/base64) based on char length
const cryptoRandomString = (charLength, encoding = "hex") => {
return require("crypto").randomBytes(Math.ceil(charLength / 2)).toString(encoding).substring(0,charLength)
}
// browser crypto (client side)
// Math.random() replacement
const cryptoRandom = () => window.crypto.getRandomValues(new Uint32Array(1))[0] / (0xffffffff + 1)
// generate random string (hex) based on char length
const cryptoRandomString = (charLength) => {
return Array.from(window.crypto.getRandomValues(new Uint8Array(Math.ceil(charLength / 2))))
.map(dec => dec.toString(16).padStart(2, "0")).join("").substring(0, charLength)
}
// use defined cryptoRandom function
// generate random number integer based on minimum and maximum
const cryptoRandomNumber = (min, max) => Math.floor(cryptoRandom() * (max - min + 1) + min)
// generate random string from a charset, based on char length
const cryptoRandomString = (charLength, charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") => {
return Array.apply(null, Array(charLength)).map( () => charset.charAt(Math.floor(cryptoRandom() * charset.length)) ).join('');
}
// generate random number integer based on minimum and maximum value defined
const randomNumber = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)
// generate random number based on number of digits defined (3 digits = number in range 100 to 999)
const randomNumberByDigits = (digits) => {
return Math.floor(Math.random() * (Math.pow(10, digits) - Math.pow(10, digits - 1) + 1) + Math.pow(10, digits - 1))
}
// or
const randomNumberByDigits = (digits) => randomNumber(Math.pow(10, digits - 1), Math.pow(10, digits))
// generate random string from a charset, based on char length
const randomString = (charLength, charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") => {
return Array.apply(null, Array(charLength)).map( () => charset.charAt(Math.floor(Math.random() * charset.length)) ).join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment