In javascript, how does random number math random generate a random number in a specified range? mathrandom

Source: Internet
Author: User

In javascript, how does random number math random generate a random number in a specified range? mathrandom

Today, a friend asked me how to generate a random number in the specified range in JavaScript. Math. random () is used to generate random numbers. However, the general reference manual does not explain how to use this method to generate random numbers within a specified range. This time I will introduce Math. random () in detail and how to use it to generate random numbers within the specified range.

W3school's random () Tutorial

Definition and usage

The random () method returns a value ranging from 0 ~ A random number between 1.

Syntax

Math. random ()

Return Value

0.0 ~ A pseudo-random number between 1.0.

Instance

In this example, we will obtain a random number between 0 and 1:

<Script type = "text/javascript"> document. write (Math. random (); </script> // output: 0.15246391076246546

How to generate a random number with a specified range value

After reading the w3school tutorial, you should know the basic usage of the Math. random () method.

Rounding data using parseInt (), Math. floor (), or Math. ceil ()

We can see that the Math. random () method is used directly to generate a number smaller than 1, so:

Math. random () * 5

The result is a random number less than 5. What we usually want to get is an integer between 0 and 5, so we need to round the result to get the expected integer. ParseInt (), Math. floor (), and Math. ceil () can all play a rounding role.

var randomNum = Math.random()*5;alert(randomNum); // 2.9045290905811183 alert(parseInt(randomNum,10)); // 2alert(Math.floor(randomNum)); // 2alert(Math.ceil(randomNum)); // 3

From the test code, we can see that the effects of parseInt () and Math. floor () are the same, all of which are down integers. So parseInt (Math. random () * 5, 10) and Math. floor (Math. random () * 5) is a random number between 0 and 4, Math. ceil (Math. random () * 5) is a random number between 1 and 5.
Generate random numbers within a specified range

Therefore, if you want to generate a random number ranging from 1 to any value, the formula is as follows:

// Max-the expected maximum value parseInt (Math. random () * max, 10) + 1; Math. floor (Math. random () * max) + 1; Math. ceil (Math. random () * max );

If you want to generate a random number ranging from 0 to any value, the formula is as follows:

// Max-the expected maximum value parseInt (Math. random () * (max + 1), 10); Math. floor (Math. random () * (max + 1); if you want to generate a random number from any value to any value, the formula is as follows: // max-the expected maximum value // min-the expected minimum value parseInt (Math. random () * (max-min + 1) + min, 10); Math. floor (Math. random () * (max-min + 1) + min );

I have learned a lot after reading this article. I hope this article will help you with the use of the random number math random. Thank you!

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.