Last active
December 21, 2017 23:46
-
-
Save shahzadns/ddf9074db1ebbffab294 to your computer and use it in GitHub Desktop.
To get an array of randomly selected alpha-numeric characters, in JavaScript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @title: Random-alphanumeric-strings | |
* @author: Shahzad Nawaz | |
* @dated: 7/16/2015. | |
*/ | |
//e.g #1 | |
var sixLenstrings = getRandomStrings(6, 2); | |
console.log(sixLenstrings); | |
// output: ["UlTl14", "JZRbSS"] | |
//e.g #2 | |
var threeLenstrings = getRandomStrings(3, 7); | |
console.log(threeLenstrings); | |
// output: ["gE3", "fO1", "AZT", "W37", "ntA", "E9Q", "hzG"] | |
//function that returns strings array. | |
function getRandomStrings(stringLen, totalStrings) { | |
var item, items, possible; | |
//possible characters to include in creating random strings - alpha-numeric | |
possible = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | |
items = []; | |
for (var i = 0; i < totalStrings; i++) { | |
item = ""; | |
for (var c = 0; c < stringLen; c++) { | |
item += possible.charAt(Math.floor(Math.random() * possible.length)); | |
} | |
items.push(item); | |
} | |
return items; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment