Generate random string/characters in JavaScript
I think this will work for you:
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));
Generate random string/characters in JavaScript
I think this will work for you:
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));
Generate random string/characters in JavaScript
I think this will work for you:
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));
Random alpha-numeric string in JavaScript?
If you only want to allow specific characters, you could also do it like this:
function randomString(length, chars) {
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
return result;
}
var rString = randomString(32, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
Here's a jsfiddle to demonstrate: http://jsfiddle.net/wSQBx/
Another way to do it could be to use a special string that tells the function what types of characters to use. You could do that like this:
function randomString(length, chars) {
var mask = '';
if (chars.indexOf('a') > -1) mask += 'abcdefghijklmnopqrstuvwxyz';
if (chars.indexOf('A') > -1) mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
if (chars.indexOf('#') > -1) mask += '0123456789';
if (chars.indexOf('!') > -1) mask += '~`!@#$%^&*()_+-={}[]:";\'<>?,./|\\';
var result = '';
for (var i = length; i > 0; --i) result += mask[Math.floor(Math.random() * mask.length)];
return result;
}
console.log(randomString(16, 'aA'));
console.log(randomString(32, '#aA'));
console.log(randomString(64, '#A!'));
Fiddle: http://jsfiddle.net/wSQBx/2/
Alternatively, to use the base36 method as described below you could do something like this:
function randomString(length) {
return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1);
}
KDB: generate random string
stringLength: 13
randomString: (1 ? .Q.A,.Q.a) , ((stringLength-1) ? .Q.nA,.Q.a)
Random string generator without repeating a combination
Just save the results in a Set and make sure that what you return at the end isn't in the Set already.
const characters ='qwertyuiopasdfghjklzxcvbnm0987654321';
function generateString(length) {
let result = ' ';
const charactersLength = characters.length;
for ( let i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
const set = new Set();
const generateUniqueString = (len) => {
let str;
do {
str = generateString(len);
} while (set.has(str));
set.add(str);
return str;
};
function captura(){
for (let i = 0; i < 50; i++) {
document.write(i+1 + ".- " + generateUniqueString(2) + '<br>');
}
}
captura();
Related Topics
How to Group an Array of Objects by Key
How to Initialize a JavaScript Date to a Particular Time Zone
When Is .Then(Success, Fail) Considered an Antipattern For Promises
Asynchronous Process Inside a JavaScript For Loop
Convert a JavaScript String in Dot Notation into an Object Reference
What Is the Use of the JavaScript 'Bind' Method
Is JavaScript Guaranteed to Be Single-Threaded
Modifying a Copy of a JavaScript Object Is Causing the Original Object to Change
How to Retrieve Get Parameters from JavaScript
What's the Difference Between "Array()" and "[]" While Declaring a JavaScript Array
How to Attach Events to Dynamic HTML Elements With Jquery
How to Merge Two Arrays in JavaScript and De-Duplicate Items
Share Data Between Angularjs Controllers
How to Select Text Nodes With Jquery
Difference Between a Function Call and Function Reference