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

Source: Internet
Author: User

The Math.random function does not produce an exponential range of data like the RAND function of PHP, and math.random only generates a pseudo-random number, which is then processed by us.today there is another netizen asked me how JavaScript generates a specified range of numeric 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 a random number within a set range.

W3school's random () tutorial
Definition and usage

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

Math.random ()

return value

A pseudo-random number between 0.0 ~ 1.0.
Instance

In this example, we will 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 W3school's tutorial, you should know the basic usage of the Math.random () method.
Rounding processing with parseint (), Math.floor (), or Math.ceil ()

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

The code is as follows Copy Code

Math.random ()

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

The code is as follows Copy Code

var randomnum = Math.random ();
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 code of the test, the effects of parseint () and Math.floor () are the same, and all are taken down to the integer part. So parseint (Math.random () *5,10) and Math.floor (Math.random ()) are all generated random numbers between 0-4, Math.ceil (Math.random ()) is the random number between the generated 1-5.
Generates a specified range of numeric random numbers

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

The code is as follows Copy Code

Max-Desired 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 from 0 to any value, this is the formula:

The code is as follows Copy Code

Max-Desired 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, this is the formula:

Max-Desired Maximum value
Min-Desired Minimum value
parseint (Math.random () * (max-min+1) +min,10);
Math.floor (Math.random () * (max-min+1) +min);

Reference:

Http://www.111cn.net/wy/js-ajax/57062.htm

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

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.