JS generation [N,m] random number

Source: Internet
Author: User

First, the preparation of knowledge

Math.ceil (); Rounding up.

Math.floor (); Rounding down.

Math.Round (); Rounded.

Math.random (); A pseudo-random number between 0.0 ~ 1.0. "Contains 0 does not contain 1"//For example 0.8647578968666494

Math. Ceil      (Math.random () *10); Gets a random integer from 1 to 10, with a minimum probability of 0.

Math. round   (Math.random ()); A random integer from 0 to 1 can be obtained evenly.

Math. Floor  (Math.random () *10); A random integer from 0 to 9 can be obtained evenly.

Math. round  (Math.random () *10); Basic equalization Gets a random integer from 0 to 10, where the probability of getting a minimum of 0 and a maximum of 10 is less than half .

Because the result in 0~0.4 is 0, 0.5 to 1.4 is 1 ... 8.5 to 9.4 is 9, 9.5 to 9.9 is 10. So there is only half of the other numbers in the distribution between the tail.

Ii. generating a random integer [n,m]

function function: Generates a random integer [N,m].

Useful when JS generates a verification code or randomly selects an option.

//Generate random numbers from Minnum to Maxnumfunction Randomnum (minnum,maxnum) {switch (arguments.length) {case 1: return parseint (Math.random () *minnum+1,10break; case 2: return parseint ( Math.random () * (maxnum-minnum+1) +minnum,10break; default: return 0; break 
Process Analysis:

Math.random () generates the number of [0,1], so

Math.random () The number of {0,5) generated.

Usually expect to get an integer, so we need to deal with the resulting results.

parseint (), Math.floor (), Math.ceil (), and Math.Round () all have integers.

Both the parseint () and Math.floor () results are rounded down.

So Math.random () is generated by a random integer [0,4].

So generate [1,max] random number, the formula is as follows:

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

So generate [0,max] to any number of random numbers, the formula is as follows:

max-Desired maximum value parseint (Math.random () * (max+1); Math.floor (Math.random () * (max+1)); 

So I want to generate [Min,max] random numbers, the formula is as follows:

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

JS generation [N,m] random number

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.