JavaScript Small instance, write a method to randomly select an integer from the number of n-m

Source: Internet
Author: User

Do not blame me is a stuffy gourd, not so many fancy language, nonsense not much to say, first talk about the requirements of small instances:

Write a method to randomly select an integer from the number of n-m, requiring: The passed parameter is less than two or not a valid number, returns the random number between [0-1], need to solve the N and m two number size problem, if m<n, two parameters of the value of exchange;

See this small instance of the random number, I believe many people will write, also wrote a lot of relevant program code, so, the important point of knowledge is not, designed to give beginners some inspiration, Daniel can skip!

Since it is for beginners to see, then we will from the most basic things step-by-step look, easy to understand, first split the request it:

1. Find a random integer of n-m;

2. The parameter is less than two or is not a valid number and returns the random number of [0-1];

3. Solve the size problem of two parameters.

Basically can be divided into these 3 parts, then we first start the simplest 0-1 random number (core function Math.random)

1 var // 0-1

Now that we can get a random number between 0-1, can we get a random number between 0-100, then we have the following:

1 var // 0-100

Why use parseint () (Take the whole function), because the resulting random number is a decimal point, take the whole can get the integer between 0-100, get 0-10 is very simple, only need to change the multiplier on it.

1 var // 0-10

So, I want to get an integer between 5 and 10, how to do it, very simple, we can split up, 5-10, the minimum is 5, the maximum is 10, then let 5+n,n as long as the 0-5 can meet the conditions, so that is

var // 5-10

Another example is to implement an integer between 50-100

var // 50-100

From the above several examples can be summed up a rule, if we want to implement n-m between the integer, it can be written like this

var num = parseint (math.random (0,1) * (m-n)) + N;

The first condition is fulfilled, and the 2nd and 3rd condition is nothing more than a condition of judgment:

functionRandomnum () {vararg = arguments;//Get Parameter Collection            varnum =NULL; if(arg.length<2 | | arg.length>2 | | isNaN (arg[0]) | | IsNaN (ARG[1])) {//when the parameter is less than 2 or greater than 2 or not a numbernum = Math.random (0,1); }Else if(Arg[0]>arg[1]) {//If the first argument is greater than the second argumentnum = parseint (math.random (0,1) * (parseint (arg[0])-parseint (arg[1])) + parseint (arg[1]); }Else{ //The first parameter is less than the second parameter, and the numeric num= parseint (Math.random (0,1) * (parseint (arg[1])-parseint (arg[0])) + parseint (arg[0]); }            returnnum; } console.log (Randomnum (' 30 ', ' 40 ');//30-40Console.log (Randomnum (30,40));//30-40

Careful people can see a lot of parseint (), first from the inside of the talk, the inside of the parseint (Arg[0]), parseint (Arg[1]) is to prevent when the number is a string, the number will be used as a string, it became the word linked connected, The outside parseint () is to take a random integer, understand?

The result is the following 2 output, if the condition is not satisfied, then return the random number between 0-1, if you want to let the argument is a string number also return 0-1, only need to add a judge, determine whether the argument is a string, is returned 0-1.

is not very simple, is so simple, today does not expand, random numbers or there are a lot of big use, have the opportunity to write about the application!

Hurry up! There is an incorrect or missing thing to understand, and hope criticizes! Appreciate it!

JavaScript Small instance, write a method to randomly select an integer from the number of n-m

Related Article

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.