// Naive solution (not recommended)
Math.random().toString(36).slice(2, 7);
// Option 1 (native node js)
var crypto = require("crypto");
var id = crypto.randomBytes(20).toString('hex');
// Option 2 (3rd party module)
var uuid = require("uuid");
var id = uuid.v4();
// Option 3 (browser native)
// dec2hex :: Integer -> String
// i.e. 0-255 -> '00'-'ff'
function dec2hex (dec) {
return dec.toString(16).padStart(2, "0")
}
// generateId :: Integer -> String
function generateId (len) {
var arr = new Uint8Array((len || 40) / 2)
window.crypto.getRandomValues(arr)
return Array.from(arr, dec2hex).join('')
}
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result;
}