Write in front:

A long time ago, before Jane wrote a few articles about random numbers, she wrote quite a mess, so I’m going to rearrange this. Js generates random numbers, generates the specified range of random numbers, and obtains non-repeating random number groups.

Js generates random numbers.

Math.random() — generates a random number greater than or equal to 0.0 and less than 1.0;

Usually, this method is used to generate random numbers. The return value of this method is a pseudo-random number, which is greater than or equal to 0.0 and less than 1.0, that is, the value range is [0.0,1.0) between closed and open, and within this range (approximately) evenly distributed.

I have a motion picture down here that you can take a look at.

Math.floor(math.random ()*20) — take a random integer between 0 and 20:

Math.floor(x) — Causes the following x (any number or expression) in parentheses to generate an integer less than or equal to x that is closest to x.

Math.floor(x), which can be understood simply as rounding down. Here are a few things to help you understand, and here is the output:

document.write(Math.floor(0.60) + "<br />")/ / 0
document.write(Math.floor(0.40) + "<br />")/ / 0
document.write(Math.floor(5) + "<br />")/ / 5
document.write(Math.floor(5.1) + "<br />")/ / 5
document.write(Math.floor(5.1) + "<br />")/ / - 6
document.write(Math.floor(5.9))/ / - 6Copy the code

Math.floor(math.random ()*20);

1. Use math.random () to generate a random number 2 between 0 and 1. And then you multiply it by 20, which is a random number from 0 to 20, but it’s not an integer. The range here can be changed, as long as the number behind the change to 50, 8, can become 0~50,0~80 random number. Math.floor(), so that the expression in parentheses (math.random () multiplied by 20) is rounded down to an integer.

Demo dynamic diagrams of the above two methods.

There is a demo link at the end of this article, so you can copy the code back and try it out for yourself

Generate a range of random numbers (from arbitrary values to arbitrary values)

We’re starting at any value, and all of the random numbers that we just picked are starting at 0. However, some scenarios may require us to select a specified range of random numbers, such as 20-30,80-120 and so on.

Here is a formula: math.floor (math.random ()*(max-min+1)+min);

Ps: Max – the maximum value in the range, min – the minimum value in the unit

If you follow this formula, you’re right. Here’s the demo chestnut.

It is worth noting:

If it is a random number in the negative range, you need to perform a sign conversion, such as –30. When you convert the sign, it becomes +. If you do not convert symbols, an error will occur!

Math.floor(Math.random()*(- 10-- 30+1) +- 30)/ / error
Math.floor(Math.random()*(- 10+30+1)- 30);/ / correct.Copy the code

Generate chestnuts for the specified range of random numbers:

Generates a chestnut for a specified range of random numbers

Get a random number set that does not repeat.

There are many ways to get a non-repeating set of random numbers, but here’s one: use indexOf() and then add the array using push().

The push() method does:

The push() method adds one or more elements to the end of the array and returns a new length.

IndexOf () : indexOf()

From the W3C

Finished or a face meng force, in the end with random number group to repeat what relationship?

Here’s a key one in particular:

Note: This method returns -1 if the string value to retrieve is not present.

Depending on this comment, through the if condition judgment, we can make the function of random number group to duplicate.

Code:

//num is an array and randomNum is a random number.
 while (num.length<3) {Num. Length indicates the length of the array. If the array length is no longer than 3, the loop is broken
       var randomNum=Math.floor(Math.random()*9);// Get a random number between 0 and 9
       if (num.indexOf(randomNum)<0) {// Check if randomNum was in the array before, if not, add it, if so, loop again.
            num.push(randomNum);
            // Add a random number to the num array.
         }
        console.log(num);
    }Copy the code

Code thread:

1. Limit the length of the array first, otherwise it becomes an infinite loop. 2. Get a random number 3. Check if it is already in the array. 4. Without this random number, indexOf () returns -1, which is used to determine the condition. 5. Break out of the loop when the random number reaches three. The num array has been deduplicated.

Code execution demo:

Here is an example of an array with a 6 in it. When the array is random to 6, push will be skipped until it is random enough to be non-repetitive.

Ps: indexOf () is relatively inefficient in the later stage, and is only applicable when the amount of data is relatively small. If the amount of data is large in the later stage, it will take a long time to compare each item in the array in sequence.

Chestnut in the text:The demo link

Above is the introduction of some random number knowledge points.

Finally: if need to reprint, please put the original link and signature. Code word is not easy, thank you for your support! Because I often do not understand others write to share, so personal writing is relatively small white, write bad place, welcome to give advice. Then is the hope of watching the friends point a like, can also pay attention to me. Ps: Currently unemployed in Beijing, I adapt to the fast pace and high intensity of the Internet, continue to learn, continue to grow, serious, rigorous, learning enthusiasm. Small and medium-sized company big guy beg to take away, mailbox: [email protected]. Nuggets personal homepage

2017.5.9