Math.random() no matching numbers code example
Example 1: random int between two numbers javascript
// Between any two numbers
Math.floor(Math.random() * (max - min + 1)) + min;
// Between 0 and max
Math.floor(Math.random() * (max + 1));
// Between 1 and max
Math.floor(Math.random() * max) + 1;
Example 2: javascript non-repeating randomize array
var nums = [1,2,3,4,5,6,7,8,9,10],//all numbers to be randomized
ranNums = [],
i = nums.length,
j = 0;
while (i--) {
j = Math.floor(Math.random() * (i+1));
ranNums.push(nums[j]);
nums.splice(j,1);
}
ranNums.next().value; //To use afterwards (for each time that the numbers are used)