Generate random string - Node.js String

Node.js examples for String:Random

Description

Generate random string

Demo Code

/**//from   www  .j  a  va2 s.  co  m
 * generate random string
 */
function randomString(len) {
  var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
  var string_length = len | 8;
  var randomstring = '';
  for (var i=0; i<string_length; i++) {
    var rnum = Math.floor(Math.random() * chars.length);
    randomstring += chars.substring(rnum,rnum+1);
  }
    return randomstring;
}

Related Tutorials