1. Generate a random number
var r = math.random (); Console.info (r);
The result is a random number of 0-1 ( returns the pseudo-random number between 0 and 1, possibly 0, but always less than 1,[0,1))
2. Generate a random integer of a specified range
/* *start < end* */ function Randomnum (start,end) { return Math.floor (Math.random () * (End-start) +start;} Console.log (Randomnum (10,90));
First of all, there is no guarantee that this is absolutely right, I think it's right.
For example (10,90) because the value generated by Math.random () is at 0-1, so the Math.random () * (End-start) range is in [0,80], and Math.floor () guarantees that the value taken is an integer. Finally add 10, it becomes the [10,90];
3. Scrambled array sorting
function Resortarr () { var length = arr.length; for (var i=0;i<length;i++) { var rnum = Math.floor (Math.random () * (length-1)); var temp = arr[i]; = Arr[rnum]; = temp; } return arr;} var arr = [1,2,3,4,5,6];console.log (Resortarr (arr));
Results
The application of random in JS