xxxxxxxxxx
function randomString(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;
}
randomString(4);
xxxxxxxxxx
//Create a function that returns a string, of a given length
const genRand = (len) => {
return Math.random().toString(36).substring(2,len+2);
}
console.log(genRand(8));
//Expected output example: 4yg020wn
xxxxxxxxxx
// program to generate random strings
const result = Math.random().toString(36).substring(2,7);
console.log(result);
xxxxxxxxxx
const randomString = () => Math.random().toString(36).slice(2)
randomString() // gi1qtdego0b
randomString() // f3qixv40mot
randomString() // eeelv1pm3ja
xxxxxxxxxx
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;
}
console.log(makeid(5));
xxxxxxxxxx
function makeid(length) {
var result = [];
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result.push(characters.charAt(Math.floor(Math.random() *
charactersLength)));
}
return result.join('');
}
console.log(makeid(5));
xxxxxxxxxx
const randomString = ()=>Math.random().toString(36) + Date.now().toString(36)
xxxxxxxxxx
function random_string(strLen=8) {
var characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
var result = ""
var charactersLength = characters.length;
for (var i = 0; i < strLen; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
xxxxxxxxxx
Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
xxxxxxxxxx
function generateRandomString(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
// Usage
const randomString = generateRandomString(10);
console.log(randomString);