The first of these methods
/** @desc: Generate random String * @remark: The ToString method can receive a cardinality as a parameter, which is capped from 2 to 36. If not specified, the default cardinality is 10 binary */function generaterandomalphanum (len) { var rdmstring = ""; for (; rdmstring.length < len; rdmstring + = Math.random (). toString (. substr (2)); Return rdmstring.substr (0, Len);}
The second method of
JS generates a GUID function, similar to NEWID () in. NET; function S4 () { return ((1 + math.random ()) * 0x10000) | 0). toString (+). substring (1); function NewGuid () { return (S4 () + S4 () + "-" + S4 () + "-" + S4 () + "-" + S4 () + "-" + S4 () + S4 () + S4 ());}
The third method of
JS generates a GUID function, similar to NEWID () in. NET; function NewGuid () { var guid = ""; for (var i = 1; i <=; i++) { var n = math.floor (Math.random () * 16.0). toString (+); GUID + = n; if ((i = = 8) | | (i = = 12) | | (i = = 16) | | (i = =)) GUID + = "-"; } return GUID;}
Fourth method
/** @desc: Generate random String * @demo: Console.log (Ranstr ()); */;(function () { //number 0-9, uppercase letter, lowercase letter, ASCII or Unicode encoding (decimal), total 62 var charcodeindex = [[48,57],[65,90],[97,122]]; var charcodearr = []; function Getbetweenround (Min,max) { return Math.floor (Min+math.random () * (max-min)); }; function Getcharcode () {for (var i=0,len=3;i<len;i++) { var thisarr = charcodeindex[i]; for (Var j=thisarr[0],thislen=thisarr[1];j<=thislen;j++) { Charcodearr.push (j);}} } function Ranstr (slen) { Slen = Slen | | charcodearr.length<62 && Getcharcode (); var res = []; for (Var i=0;i<slen;i++) { var index = getbetweenround (0,61); Res.push (String.fromCharCode (Charcodearr[index)); } Return Res.join ("); }; This.ranstr = Ranstr;}) ();
The various functions of JS generating random numbers