10 examples of 'javascript random string from array' in JavaScript

Every line of 'javascript random string from array' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
109function getRandomString(length, chars) {
110 var string = '';
111 for (var i = length; i > 0; --i) string += chars[Math.floor(Math.random() * chars.length)];
112 return string;
113}
84export function randomString(length = 8) {
85 return [...Array(length)].map(() => (~~(Math.random() * 36)).toString(36)).join("")
86}
130randomString() {
131 return lipsum[Math.floor(Math.random() * 100)]
132}
19function randomString() {
20 const length = (Math.floor(Math.random() * 25) + 10);
21 let str = '';
22 for (let i = 0; i < length; i += 1) {
23 str += String.fromCharCode(Math.floor(Math.random() * 255))
24 }
25 return str;
26}
62function randomString(length, chars) {
63 var mask = "";
64 if (chars.indexOf("a") > -1) mask += "abcdefghijklmnopqrstuvwxyz";
65 if (chars.indexOf("A") > -1) mask += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
66 if (chars.indexOf("#") > -1) mask += "0123456789";
67 if (chars.indexOf("!") > -1) mask += '~`!@#$%^&*()_+-={}[]:";\"<>?,./|\\';
68 var result = "";
69 for (var i = length; i > 0; --i) {
70 result += mask[Math.floor(Math.random() * mask.length)]
71 }
72 return result;
73}
42public static stringRandom(chars: number): string{
43 let text = "";
44 let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
45
46 for( let i = 0; i < chars; i++ )
47 text += possible.charAt(Math.floor(Math.random() * possible.length));
48
49 return text;
50}
9export function randomString(length) {
10 const buffer = [];
11 const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
12 for (let i = 0; i < length; i++) {
13 buffer.push(chars[getRandomInt(chars.length)]);
14 }
15 return buffer.join('');
16}
31function randomString(len) {
32 const buf = [],
33 chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
34 charLen = chars.length;
35
36 for (let i = 0; i < len; ++i) {
37 buf.push(chars[getRandomInt(0, charLen - 1)]);
38 }
39
40 return buf.join('');
41}
20function randomString() {
21 var text = "";
22 var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
23
24 for (var i = 0; i < 2; i++)
25 text += possible.charAt(Math.floor(Math.random() * possible.length));
26
27 return text;
28}
29export function randomString () {
30 var string = ''
31 var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
32
33 for (var i = 0; i < 5; i++) { string += possible.charAt(Math.floor(Math.random() * possible.length)) }
34
35 return string
36}

Related snippets