This time I will bring you a detailed explanation of the steps for generating random range and random sequence in JS. What are theprecautions for generating random range and random sequence in JS? Here is a practical case, let’s take a look at it together.
In •1.Math.random(); The result is a random number between 0-1 (including 0, excluding 1) •2. Math.floor(num); The parameter num is a numerical value, and thefunctionresult is theintegerpart of num. The same as the parseInt(num) method for taking integers.
•3.Math.round(num); The parameter num is a numerical value, and the function result is the integer after num is rounded. •4.Math.ceil(num); Returns the smallest integer greater than or equal to n. Reasonably use the above method to generate a random number in the specified range: Including the minimum value but not the maximum value: ParseInt(num) rounds the incoming Convert num to a small integer. Multiply Math.random() by the difference between the maximum and minimum, process it with parseInt (note that the range at this time is [0-difference)), and add the minimum value, you can get the minimum value but not the maximum Integer value. To obtain a random number that contains the minimum value but not the maximum value, use the following function:function getRandom1(start, end) { var length = end - start; var num = parseInt(Math.random() * (length) + start); return num; }
Math.ceil(num)Get the smallest integer greater than or equal to num, that is, convert the passed num into a large integer. Multiply Math.random() by the difference between the maximum and minimum, process it with Math.ceil() (note that the range at this time is (0-difference]), and add the minimum value, you can get the minimum value excluding the minimum value. Value but including the maximum value of the integer. That is, change the parseInt in the above situation to Math.ceil()
function getRandom1(start, end) { var length = end - start; var num = Math.ceil(Math.random() * (length) + start); return num; }
function getRandom1(start, end) { var length = end - start + 1; var num = parseInt(Math.random() * (length) + end); return num; }
var numArr = getNum().sort(function () { return Math.random() - 0.5; });
function getOrder(start, end) { var length = end - start; var myorder = new Array(); var index = 0; while (index < length+1) { var flag = true; var num = parseInt(Math.random() * (length + 1)); for (var i in myorder) { if (myorder[i] == num) { flag = false; } } if (flag == true) { myorder[index] = num; index++; } } alert(myorder.length); alert(myorder); } getOrder(5, 10);
What are the ways to automate vue forms
How does vue make password changes and personal Information viewing function
The above is the detailed content of Detailed explanation of the steps to generate range randomization and sequence randomization in JS. For more information, please follow other related articles on the PHP Chinese website!