Home > Web Front-end > JS Tutorial > Detailed example of how to use JavaScript to randomize the value index in an array and create a random array

Detailed example of how to use JavaScript to randomize the value index in an array and create a random array

巴扎黑
Release: 2017-09-14 11:59:11
Original
1523 people have browsed it

The example of this article describes the method of javascript to randomize the value index in the array and create a random array. 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" one I had implemented before. method. 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.

Regarding this array problem (then sort the values ​​in an array and return a new array), my previous implementation method was like this:

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;
}
Copy after login

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 taken. If not, the value of the index will be Put it into a new array, and if it has been retrieved, increase the decrement key i by 1 (the purpose is to repeat the cycle until another index that has not been retrieved is retrieved). 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 classmate 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;
}
Copy after login

This is a quite clever algorithm. After taking a random index in each loop, and changing its value from Delete from the array, so that if this index is still randomly fetched later, this index will no longer be the value fetched last time, and the value range of the random number will decrease according to the length of the array, so You can cycle a certain number of times at one time and get the desired result.

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 large shuffling algorithm, that is, changing each deletion operation to a position replacement operation ( The value of the index obtained is exchanged with the value corresponding to the current decrementing key i), so that the impact on the entire array is minimal. 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;
}
Copy after login

Finally give a "create value" is a random array between min~max" method, 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;
}
Copy after login

The above is the detailed content of Detailed example of how to use JavaScript to randomize the value index in an array and create a random array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template