1var crypto = require("crypto");
2var id = crypto.randomBytes(20).toString('hex');
3
4// "bb5dc8842ca31d4603d6aa11448d1654"
1function makeid(length) {
2 var result = [];
3 var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
4 var charactersLength = characters.length;
5 for ( var i = 0; i < length; i++ ) {
6 result.push(characters.charAt(Math.floor(Math.random() *
7 charactersLength)));
8 }
9 return result.join('');
10}
11
12console.log(makeid(5));
11.
2function makeid(length) {
3 var result = '';
4 var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
5 var charactersLength = characters.length;
6 for ( var i = 0; i < length; i++ ) {
7 result += characters.charAt(Math.floor(Math.random() *
8 charactersLength));
9 }
10 return result;
11}
12
132.
14let r = (Math.random() + 1).toString(36).substring(7);
15console.log("random", r);
16
173.
18function randomString(len) {
19 var str = ""; // String result
20 for (var i = 0; i < len; i++) { // Loop `len` times
21 var rand = Math.floor(Math.random() * 62); // random: 0..61
22 var charCode = rand += rand > 9 ? (rand < 36 ? 55 : 61) : 48; // Get correct charCode
23 str += String.fromCharCode(charCode); // add Character to str
24 }
25 return str; // After all loops are done, return the concatenated string
26}
27console.log(randomString(10));