1var crypto = require("crypto");
2var id = crypto.randomBytes(20).toString('hex');
3
4// "bb5dc8842ca31d4603d6aa11448d1654"
1function randomString(length) {
2 var result = '';
3 var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=-';
4 var charactersLength = characters.length;
5 for ( var i = 0; i < length; i++ ) {
6 result += characters.charAt(Math.floor(Math.random() * charactersLength));
7 }
8 return result;
9 }
10randomString(4);
1function getRandomString(length) {
2 var randomChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
3 var result = '';
4 for ( var i = 0; i < length; i++ ) {
5 result += randomChars.charAt(Math.floor(Math.random() * randomChars.length));
6 }
7 return result;
8}
9
10//usage: getRandomString(20); // pass desired length of random string
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 += characters.charAt(Math.floor(Math.random() *
7 charactersLength));
8 }
9 return result;
10}
11
12console.log(makeid(5));
1// program to generate random strings
2
3const result = Math.random().toString(36).substring(2,7);
4console.log(result);
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));