Random Number Generation by js: random sample of the random function _ javascript skills

Source: Internet
Author: User
JavaScript can use JavaScriptMath. random () built-in function to generate random numbers. The following is a good example. You can try to operate the JavaScript Math. random () built-in function.
Random Function Return Value
Returns a pseudo-random number between 0 and 1, which may be 0, but always less than 1)
Random Function example
// Returns a random number.
Document. write (Math. random ());
// Returns a random number ranging from 10 to 20.
Document. write (Math. random () * (20-10) + 10 );
// Return the formula of the random number (m-n) in the specified range
Document. write (Math. random () * (n-m) + m );
Time-based random numbers can also be generated

The Code is as follows:


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.

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.