Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,111 questions

40,781 answers

573 users

How to generate random string in JavaScript

4 Answers

0 votes
function generate_random_string(size) {
    let characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    let randomChars = "";
 
    for (let i = 0; i < size; i++) {
        randomChars += characters.charAt(Math.floor(Math.random() * characters.length));
    }
 
    return randomChars;
}

console.log(generate_random_string(10));

console.log(generate_random_string(10));

 
 
/*
 
run:
 
ftYpjfMtrS
tmOjeYAwac
 
*/

 
 

 





answered Apr 7, 2016 by avibootz
edited 3 days ago by avibootz
0 votes
// base 36 - [0-9a-z]

console.log((Math.random() + 1).toString(36).substring(2, 9));

console.log((Math.random() + 1).toString(36).substring(2, 5));
 
 
 
/*
 
run:
 
vzadiqx
f0s
 
*/

 





answered Apr 7, 2016 by avibootz
edited 3 days ago by avibootz
0 votes
console.log((Math.random() + 1).toString(36).substring(7));

console.log((Math.random() + 1).toString(36).substring(5));

console.log((Math.random() + 1).toString(36));


  
/*
  
run:
  
vunly
7zlo277
1.epxxxzsmno
  
*/

 





answered Feb 16, 2017 by avibootz
edited 3 days ago by avibootz
0 votes
let crypto = require("crypto");
 
let rnd = crypto.randomBytes(30).toString('hex');
 
 
console.log(rnd);
  
  
  
/*
run:
      
228a9960f27687aff31332059a3fd2851906317e05334db1bd81e3d713d5
 
*/

 





answered 3 days ago by avibootz
...