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

Source: Internet
Author: User

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

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:

`<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:

`1 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.

`1 var randomnum = Math.random (); 2 //  3//  24//  25//  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:

`1 // max-desired maximum value 2 parseint (Math.random () *max,10) +1; 3 Math.floor (Math.random () *max) +1; 4 Math.ceil (Math.random () *max);`

If you want to generate random numbers from 0 to any value, this is the formula:

`1 // max-desired maximum value 2 parseint (Math.random () * (max+1), ten); 3 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);

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

Related Keywords:

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.