The example in this article describes the method of randomizing the value index in the array and creating a random array using JavaScript. Share it with everyone for your reference. The details are as follows:
Today in the QW communication group, I saw some students discussing the issue of randomizing arrays. The algorithm given was very good, and it reminded me of the not so "beautiful" method I had implemented before. Think about it, sometimes when we are busy writing business code just to implement its function, we don't spend much thought on whether there is a better implementation method.
As far as this array problem is concerned (then sort the values in an array and return a new array), my previous implementation method was as follows:
function randArr(arr) { var ret = [], obj = {}, i = arr.length, l = i, n; while (--i >= 0) { n = Math.floor( Math.random() * l ); if (obj[n] === void 0) { ret[ret.length] = obj[n] = arr[n]; } else { i++; } } return ret; }
The above code will work, but it is not a good algorithm. It intends to execute the "length of the original array" loop. Each loop will randomly pick an index in the original array, and then determine whether the index has been fetched. If not, put the value of the index into the new array. If it has been, increase the decrement key i by 1 (the purpose is to repeat the cycle until another index that has not been fetched is fetched). The performance of this method depends on your character. I believe that students who have seen this idea will understand the reason.
Now give the algorithm of the student in the group:
function randArr(arr) { var ret = [], i = arr.length, n; arr = arr.slice(0); while (--i >= 0) { n = Math.floor( Math.random() * i); ret[ret.length] = arr.splice(n, 1)[0]; } return ret; }
This is a quite clever algorithm. After each loop, a random index is taken and its value is deleted from the array. In this way, if the index is still randomly taken later, the index is no longer It is the value obtained last time, and the range of random numbers will decrease according to the decrease of the length of the array, so that the ideal result can be obtained by looping a certain number of times at one time.
I also saw an improved version, which took into account some performance problems caused by the deletion operation of the array and used JK's shuffling algorithm, that is, changing each deletion operation to a position replacement operation (the fetched The value of this index is interchanged with the value corresponding to the current decrementing key i), so that the impact on the entire array is minimal, so let’s put the code:
function randArr(arr) { var ret = [], i = arr.length, n; arr = arr.slice(0); while (--i >= 0) { n = Math.floor( Math.random() * i); ret[ret.length] = arr[n]; arr[n] = arr[i]; } return ret; }
Finally, a method of "creating a random array with a value between min~max" is given. The algorithm principle is similar to the above:
function makeRandArr(min, max) { var ret = [], obj = {}, n; for (; max >= min; max--) { n = Math.ceil( Math.random() * (max - min) ) + min; ret[ret.length] = obj[n] || n; obj[n] = obj[max] || max; } return ret; }
I hope this article will be helpful to everyone’s JavaScript programming design.