Javascript automatically generates random strings containing numbers and characters, and javascript automatically generates

Source: Internet
Author: User

Javascript automatically generates random strings containing numbers and characters, and javascript automatically generates

This example describes how javascript automatically generates random strings containing numbers and characters. Share it with you for your reference. The details are as follows:

Math. random () and Math. floor () are mainly used here.

Math. random () -- returns a pseudo-random number between 0 and 1, which may be 0, but always less than 1)

Math. floor () -- round down and remove the value after the small number

How to Implement Random Multiple Digits:

Copy codeThe Code is as follows :/**
*
* Randomly generate numbers
*
* @ Param num: number of digits generated
*/
Function randomNumber (num ){
Return ''+ Math. floor (Math. random () * num );
}

Javascript randomly generated characters

The following examples are random characters in the random A-Z/a-z range
Copy codeThe Code is as follows :/**
*
* Random generation
*
* @ Param data json data example: {"start": 0, "end": 2, "number": 5, "upper": true}
* @ Param start subscript
* @ Param end subscript
* @ Param number: number of digits generated
* @ Param upper whether to use uppercase letters is lowercase by default
*/
Function randomLetter (data ){
Var letterData = "";
Var lowercase = new Array ("a", "B", "c", "d", "e", "f", "g", "h ", "I", "g", "k", "l", "m", "n", "o", "p", "q", "r ", "s", "t", "u", "v", "w", "x", "y", "z ");

Var uppercase = new Array ("A", "B", "C", "D", "E", "F", "G", "H ", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R ", "S", "T", "U", "V", "W", "X", "Y", "Z ");
Var start = data. start;
Var end = data. end;
Var number = data. number;
Var upper = data. upper;

Var length = end-start;
For (var I = 0; I <number; I ++ ){
Var index = Math. floor (Math. random () * length) + start;
Var letter = "";
If (upper ){
Letter = uppercase [index];
} Else {
Letter = lowercase [index];
}
LetterData + = letter;
}
Return letterData;
}

Reference:
Copy codeThe Code is as follows: alert (new Date (). Format ("yyyy-MM-dd hh: mm: ss "));

I hope this article will help you design javascript programs.

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.