[Reserved] using Javascript Math.random () function generates a random number a random number from 1 to 1000

In the process of writing Javascript code, sometimes we need to use to generate random numbers Js, ajax get request to clear the cache when we will take a random number to solve this problem, in addition to the random number may also be used in other applications, in Javascript language, Math.random () function is designed to generate a random number, random () method returns a random number between 0 and 1, if desired using JavaScript to generate 1-100 or 1-1000 a random number range, may be used Math.random () multiplied by 100 or 1000 to achieve.

(1) generating a random number in the 0 to 1 range

 Math.random();

(2) generating a random number in the range 0-100

Math.ceil(Math.random()*100);  

(3) generating a random number in the range 0 to 1000

Math.ceil(Math.random()*100);  

In Examples 2 and 3 described above, a function to Math.ceil, Math.ceil function represents rounding up, i.e. the value which is always rounded up to the nearest integer.

 

Note: The text reproduced from personal bloggers station IT technology small fun house , the original link to Javascript using Math.random () function generates a random number random number _IT technology small house 1-1000 of interest .

Guess you like

Origin www.cnblogs.com/xu-yi/p/10993628.html