JavaScript code with examples.
// Generate array of random strings (A-Z, 0-9, A-Z0-9)
// Author: Matej Lednár
/**
* @param {Number} stringLength - undefined random, number fixed
* @param {Number} charType - 1 a-Z, 2 0-9, 3 a-Z0-9
* @param {Number} stringType - 0 mix, 1 lowercase, 2 uppercase
* @returns
*/
function generateData(stringLength, charType, stringType) {
let finalResult = [];
const charactersAZ = "abcdefghijklmnopqrstuvwxyz";
const characters09 = "0123456789";
let characters = "";
charType = charType ? charType : 1;
switch (charType) {
case 1:
characters = charactersAZ;
break;
case 2:
characters = characters09;
break;
case 3:
characters = charactersAZ + characters09;
break;
}
let strLength = stringLength
? stringLength
: Math.floor(Math.random() * 9 + 1);
function generateString(length) {
let result = "";
const charsLength = characters.length;
for (let i = 0; i < length; i++) {
stringType = stringType ? stringType : 0;
switch (stringType) {
case 0:
lowerCase =
Math.floor(Math.random() * 2) == 1 ? true : false;
break;
case 1:
lowerCase = true;
break;
case 2:
lowerCase = false;
break;
}
let char = characters.charAt(
Math.floor(Math.random() * charsLength)
);
result += lowerCase ? char : char.toUpperCase();
}
return result;
}
// generate variable string (1-10 characters per string)
for (let i = 1; i < 50; i++) {
finalResult.push(generateString(strLength));
}
return finalResult;
}
console.log(generateData());
console.log(generateData(5));
console.log(generateData(5, 2));
console.log(generateData(5, 3));
console.log(generateData(5, 3, 0));
console.log(generateData(5, 3, 1));
console.log(generateData(5, 3, 2));
Example of the output
[
'RSoAhqfZy', 'yAvyUpkzN', 'cxkwCJtdP', 'tpkockMJE',
'IwcYrPpdq', 'DgqiLCYqA', 'APvNbNosH', 'HmsVixycN',
'VAOGJzjJk', 'fXRMoezHE', 'LPpxsGwAx', 'NDloZYOrn',
'FFLbxpxSL', 'OGuPxMZRp', 'FdHtlDado', 'VVKEQQvgM',
'yYkYcWwUR', 'mmQXsUhRI', 'niMehTDel', 'YppAfBpQW',
'LVHqxmNQd', 'oBlLteeCj', 'tzMkUfCiz', 'PKBjdcFKp',
'dOGRjfcKH', 'RiTCDbiUp', 'eroYIYiUb', 'JBdJqxuBF',
'EIfBzNiSI', 'kEpNUNuyB', 'cSxNXAIui', 'lAHBcANKi',
'tJRpXnfrf', 'vRCkTIwaF', 'lCpiUXyLy', 'fIMbPaLvJ',
'YorHqUjSa', 'iPaBRSsgs', 'usdaaPghe', 'HgYaWtDHO',
'mFTTLrYsL', 'zzisRMnJy', 'dabUPutcO', 'EosRlnkBM',
'ZELtfcowl', 'UoyAOWDfr', 'veazXfMRc', 'SIxftYHkQ',
'btvMxBWwr'
]
[
'wSUMg', 'pUxhs', 'KkgTC', 'zXYBZ', 'IVRhb',
'rSzcK', 'FyXyT', 'dSPiE', 'iwSvw', 'mCYiG',
'ZYHaC', 'RNZcA', 'HvXZO', 'iRtKk', 'AcFuh',
'vFHdS', 'JJXHG', 'ewPpI', 'KmHEX', 'mmzMA',
'tcuHV', 'yKklI', 'JuVrx', 'lXlxF', 'WrzpC',
'kodlT', 'LUPVA', 'RhIju', 'JSOWM', 'dyTws',
'RxJdq', 'RsZuF', 'RpyEt', 'MIvgM', 'QEmph',
'HTyMR', 'MsvxZ', 'WDkEk', 'JCMfh', 'HaUQU',
'OHweN', 'qlhIw', 'CsHnF', 'WPaVU', 'bmixz',
'cxbyn', 'xyREH', 'rKKVZ', 'UUtFX'
]
[
'54183', '05410', '08209', '84971', '98415',
'70026', '24769', '60334', '37790', '88085',
'99439', '65172', '93528', '03917', '84222',
'43713', '38958', '67030', '58797', '52769',
'48495', '61117', '51215', '21674', '69242',
'48780', '08494', '22960', '80848', '38689',
'12363', '11800', '04826', '71622', '32834',
'90386', '37425', '08521', '40445', '36718',
'42212', '44980', '98728', '66037', '89847',
'24742', '57420', '81292', '09487'
]
[
'9j0AO', 'FyeU4', '5pj2h', 'IRVq0', 'iR9Ay',
'iAKt8', 'kVcbS', 'N9W98', 'PB7bn', 'Hnr2w',
'OUjc5', 'h57e1', 'MXWQl', 'sB1B7', 'Ea00A',
'8tLvZ', 'iW8Dh', 'KXO5N', 'dAtgB', 'N381W',
'7gxfK', 'liVNG', 'Q6V07', 'YdFkP', '0xrtu',
'vb0n9', '2k47j', 'Ayrj4', 'PJwqg', 'u7TPi',
'o09kV', 'Aqv8p', '42wx4', '40GVP', 'JVh82',
'378Qu', '2Lvty', '87Miu', 'WLTcx', 'Tu43T',
'L8Vsa', 'SDUU2', 'KPBmp', 'mm3Se', 'tb7cl',
'9tC4j', 'U034g', 'MQRE5', '8kQf5'
]
[
'02OQ1', 'Cx84t', '5vWRl', 'jcBpi', 'TE8N2',
'md4JE', 'kyWuZ', 'FG36b', 'M7uh1', '2Tpm5',
'vG2K8', 'jdv5Z', '8ipdI', 'cfsQ2', 'bRtXO',
'Bz8mT', '9P869', 'd8YD1', 'DVqek', 'G43Zs',
'R9NjN', 'A31O4', 'wDX34', 'YNbC5', 'U2Uvh',
'6eMtl', '1jZlQ', 'tE134', 'EW106', 'Mlm5x',
'GIqL4', 'T9H90', 'UHZem', 'rI9y5', '0wfqZ',
'5zlC0', '7K6Rd', '50HpB', 'V8cnS', 'ocvfa',
'3KpR5', 'xETpV', '6sB8v', '9pQQW', 'F4ADc',
'6x7gE', 'S0exG', 'yA9pV', 'Kv7Ld'
]
[
'ymowy', '6ai5w', '2qmxx', '3uvxy', 'xu9se',
'kyml0', 'htp0d', 'aom0w', 'zfgia', '8ebik',
'w08o8', 'fmewt', '7of8i', '8ipf7', 'utm0k',
'ac4jj', 'y6jii', '1dka4', 'silu4', '7u3pk',
'uyova', 'm8noq', '1krrq', '4cu4k', '4b41r',
'msda1', 'r5jcg', 'o1apr', 'ync7t', 'p3684',
'gxjym', 'gbq3w', 'kbo3t', '0wkvv', 'ppu2d',
'y4ub9', 'z7tqy', 'lkmo2', 'qhotb', '2rlxf',
'8zesn', 'xqk1v', 'pyurb', 'ouai3', '1kmq2',
'75lq7', 'gx0mt', '0bjmx', 'azjse'
]
[
'N7W12', '5ARGE', 'TEW49', 'WYT9H', 'U9JOO',
'KHA50', 'FQZ3E', 'VIZJB', 'FIVKC', '8G1DV',
'XZWVP', 'ZNP2G', 'PJGAM', 'AHM3X', 'SZRBV',
'0NNFA', '1DHNI', 'FZGJW', '0ID1U', 'FCLSQ',
'D9RO9', '5M9R1', 'HXTB3', 'FSKHS', 'Y01N6',
'E792R', '123EA', 'UR3CD', 'APTU3', 'UXQF9',
'PSIFK', '74WO9', 'U8AZQ', 'C2UBA', 'X17YG',
'4C152', 'E69JW', 'IV05J', 'IZVRV', 'H3U9G',
'QR9AH', '9ASIH', 'YC2IJ', 'BVMXJ', 'O8A9M',
'YY7WW', '9XI7E', 'L72W5', 'L6Q0U'
]
Conclusion
For more information visit my GitHub repository.