This article mainly introduces four types of UDFs used to generate random numbers in JavaScript. This article describes four methods and provides four code snippets at the same time. For more information, see
Method 1
/** @ Desc: generates a random string * @ remark: the toString method can receive a base number as a parameter. the base number is capped from 2 to 36. If this parameter is not specified, the default base number is 10 x/function generateRandomAlphaNum (len) {var rdmString = ""; for (; rdmString. length <len; rdmString + = Math. random (). toString (36 ). substr (2); return rdmString. substr (0, len );}
Method 2
// JS generates the GUID function, similar. newID (); function S4 () {return (1 + Math. random () * 0x10000) | 0 ). toString (16 ). substring (1);} function NewGuid () {return (S4 () + S4 () + "-" + S4 () + "-" + S4 () + "-" + S4 () + "-" + S4 () + S4 () + S4 ());}
Method 3
// JS generates the GUID function, similar. newID (); function newGuid () {var guid = ""; for (var I = 1; I <= 32; I ++) {var n = Math. floor (Math. random () * 16.0 ). toString (16); guid + = n; if (I = 8) | (I = 12) | (I = 16) | (I = 20) guid + = "-";} return guid ;}
Method 4
/** @ Desc: generates a random string * @ demo: console. log (ranStr (); */; (function () {// number 0-9, uppercase letters, lowercase letters, ASCII or UNICODE encoding (decimal ), A total of 62 var charCodeIndex = [[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