10 examples of 'generate random characters in javascript' in JavaScript

Every line of 'generate random characters in javascript' 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
this disclaimer
24function generate(chars) {
25 let astral = [], re = ""
26 for (let i = 0, at = 0x10000; i < chars.length; i++) {
27 let from = chars[i], to = from
28 while (i < chars.length - 1 && chars[i + 1] === to + 1) {i++; to++}
29 if (to <= 0xffff) {
30 if (from === to) re += esc(from)
31 else if (from + 1 === to) re += esc(from) + esc(to)
32 else re += esc(from) + "-" + esc(to)
33 } else {
34 astral.push(from - at, to - from)
35 at = to
36 }
37 }
38 return {nonASCII: re, astral: astral}
39}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

20export function getRandomChar(): string {
21 let n = Math.ceil(Math.random() * 100)
22 if (n <= 40) return String.fromCharCode(Math.floor(Math.random() * 26) + 65) // Majuscule
23 if (n <= 80) return String.fromCharCode(Math.floor(Math.random() * 26) + 97) // Minuscule
24 return String.fromCharCode(Math.floor(Math.random() * 10) + 48) // Numero
25}
86function getRandomChar(){
87 var randKey = round(random(65, 90));
88 char = String.fromCharCode(randKey);
89 char = char.toUpperCase();
90 return char;
91}
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}
46export default function generateString(length = 10) {
47 let key = "";
48 for (let i = 0; i < length; i++) {
49 key += getRandomChar();
50 }
51 return key + checksum(key);
52}
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}
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}
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}
44function randomChar() {
45 return CHARS[randomNumber(0, CHARS.length - 1)];
46}
13generateCode(length) {
14 for (var i = 0; i < length; i++) {
15 this.code += String.fromCharCode(Math.floor(Math.random() * 255));
16 }
17}

Related snippets