Javascript random number and advanced app-vbscript (asp) Random Number summary_javascript skills

Source: Internet
Author: User
Sometimes I forget the random number function of the program or the random number of javascript and vbscript. I would like to summarize the random number function of the two for future convenience. 1. javascript random number function Math. random ()
Generate random numbers within a specified range
The Math. random () method has no parameters. 0 ~ is returned ~ Random number between 1. If 0 ~ is to be generated ~ Random between n
Number, which can be in the following format:

Math. floor (Math. random () * n)
To generate m ~ Random Number between n, which can be used:
Math. floor (Math. random () * n) + m

The following is an application:

<Script language = "javascript" type = "text/javascript"> <! -- // Generate a random number ranging from 0 to 1000 var I = Math. floor (Math. random () * 1000); document. write (I); document. write ("<br>") // generate 1 ~ Random Number between 10 var j = Math. floor (Math. random () * 10) + 1; document. write (j); // --> script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]



Javascript random number advanced application
① Since JavaScript was generated, many browsers have built-in random number generation methods. For example:
Var number = Math. random ();
This method generates a floating point number between 0 and 1.
② Based on Time, random numbers can also be generated. For example:
Var now = new Date ();
Var number = now. getSeconds ();
This generates an integer ranging from 0 to 59 based on the current time.
Var now = new Date ();
Var number = now. getSeconds () % 43;
This generates an integer ranging from 0 to 42 based on the current time.
③ Here we will introduce a fairly good random number generator program, which can be applied in many fields.

The Code is as follows:


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.