Share the four functions implemented by JS to generate random numbers. js implements random number functions.

Source: Internet
Author: User

Share the four functions implemented by JS to generate random numbers. js implements random number functions.

Method 1
Copy codeThe Code is as follows:
/*
* @ Desc: generates a random string.
* @ Remark: the toString method can receive a base number as a parameter. This base number is capped from 2 to 36. If not specified, the default base is in decimal format.
*/
Function generateRandomAlphaNum (len ){
Var rdmString = "";
For (; rdmString. length <len; rdmString + = Math. random (). toString (36). substr (2 ));
Return rdmString. substr (0, len );
}

Method 2

Copy codeThe Code is as follows:
// JS generates the GUID function, similar to NewID () in. net ();
Function S4 (){
Return (1 + Math. random () x 0x10000) | 0). toString (16). substring (1 );
}

Function NewGuid (){
Return (S4 () + S4 () + "-" + S4 () + "-" + S4 () + "-" + S4 () + "-" + S4 () + S4 () + S4 ());
}

Method 3

Copy codeThe Code is as follows:
// JS generates the GUID function, similar to NewID () in. net ();
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

Copy codeThe Code is as follows:
/*
* @ Desc: generates a random string.
* @ Demo: console. log (ranStr ());
*/
; (Function (){
// Numbers 0-9, uppercase letters, lowercase letters, ASCII or UNICODE (decimal), 62 in total
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 <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 | 20;
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;
})();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.