Math.random () in JS generates a random number of values in a specified range

Source: Internet
Author: User

Today, another netizen asked me how JavaScript generates a specified range of numerical random numbers. Math.random () This method is believed to be known to generate random numbers. However, the general reference manual does not show how to use this method to generate random numbers within a specified range. This time I'll give you a detailed introduction to Math.random () and how to use it to generate random numbers within the scope of the development.
W3school's random () tutorial
Definitions and usage

The random () method can return a random number between 0 ~ 1.
Grammar

Math.random ()

return value

A pseudo random number between 0.0 ~ 1.0.
Instance

In this case, we'll get a random number between 0 and 1:

The code is as follows Copy Code

<script type= "Text/javascript" >
document.write (Math.random ());
</script>

Output:
0.15246391076246546

How to generate a random number for a specified range value

After reading the W3school tutorial, you should know the basic usage of the Math.random () method.
Rounding with parseint (), Math.floor () or Math.ceil ()

We see that the direct use of the Math.random () method produces a number less than 1, so:

The code is as follows Copy Code

Math.random () *5

The result is a random number less than 5. And what we usually want is an integer between 0-5, so we need to round up the resulting results and get the integers we expect. parseint (), Math.floor () and Math.ceil () can all be rounded up.

The code is as follows Copy Code

var randomnum = Math.random () *5;
alert (randomnum); 2.9045290905811183
Alert (parseint (randomnum,10)); 2
Alert (Math.floor (Randomnum)); 2
Alert (Math.ceil (Randomnum)); 3

As we can see from the test code, the effect of the parseint () and Math.floor () is the same, and all the parts of the integer are taken down. So parseint (Math.random () *5,10) and Math.floor (Math.random () *5) are random numbers between 0-4 generated, Math.ceil (Math.random () *5) is the number of random numbers between 1-5 generated.
Generate a specified range of numeric random numbers

So, if you want to generate random numbers of 1 to any value, the formula is this:

The code is as follows Copy Code

Max-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 random numbers of 0 to any value, the formula is this:

The code is as follows Copy Code

Max-expected maximum value
parseint (Math.random () * (max+1), 10);
Math.floor (Math.random () * (max+1));

If you want to generate random numbers of arbitrary values to any value, the formula is this:

Max-expected maximum value
Min-Expected minimum value
parseint (Math.random () * (max-min+1) +min,10);
Math.floor (Math.random () * (max-min+1) +min);

What do you think? Now it should be clear how to generate the random numbers you need?! Hope to read this article to your development to help! This time it's over here!

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.