1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| function randomString(len) { const CHARACTERS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; const BASE = CHARACTERS.length; let result = '';
while (result.length < len) { let randomNum = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER); let randomPart = toBase62(randomNum, CHARACTERS, BASE); result += randomPart; }
return result.slice(0, len); }
function toBase62(value, characters, base) { let result = ''; do { const index = value % base; result = characters[index] + result; value = Math.floor(value / base); } while (value > 0);
return result; }
console.log(randomString(11));
|