Answers for "javascript how to generate random string"

23

javascript generate random string

var crypto = require("crypto");
var id = crypto.randomBytes(20).toString('hex');

// "bb5dc8842ca31d4603d6aa11448d1654"
Posted by: Guest on July-16-2020
4

how to generate random character from an array js

function makeid() {
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < 5; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}

console.log(makeid());
Posted by: Guest on December-08-2019
2

how to generate random string in javascript

Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
Posted by: Guest on June-13-2020
-1

how to generate random strings in js/Node/Javascript

1.
function makeid(length) {
    var result           = '';
    var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var charactersLength = characters.length;
    for ( var i = 0; i < length; i++ ) {
      result += characters.charAt(Math.floor(Math.random() * 
 charactersLength));
   }
   return result;
}

2.
let r = (Math.random() + 1).toString(36).substring(7);
console.log("random", r);

3.
function randomString(len) {
  var str = "";                                // String result
  for (var i = 0; i < len; i++) {              // Loop `len` times
    var rand = Math.floor(Math.random() * 62); // random: 0..61
    var charCode = rand += rand > 9 ? (rand < 36 ? 55 : 61) : 48; // Get correct charCode
    str += String.fromCharCode(charCode);      // add Character to str
  }
  return str; // After all loops are done, return the concatenated string
}
console.log(randomString(10));
Posted by: Guest on September-24-2021

Code answers related to "javascript how to generate random string"

Code answers related to "Javascript"

Browse Popular Code Answers by Language