JavaScript implementation of array index randomization and method of creating random arrays _javascript Tips

Source: Internet
Author: User
Tags arrays

In this paper, we describe the method of the JavaScript implementation of the index randomization and the creation of random array. Share to everyone for your reference. Specifically as follows:

Today in the QW Exchange group to see a classmate to discuss the problem of making array randomization, which gives the algorithm is very good, let me think of the previous implementation of the less "beautiful" method. Think about the time when we're busy writing business code just to make it work, and we're not thinking too much about whether there's a better way to implement it.

For this array problem (and then sort the values in an array and return a new array), my previous implementation was 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;
}

The code above will work, but it's not a good algorithm, it intends to perform the "length of the original array" secondary loop, each time the loop will randomly take an index in the original array, and then determine whether the index has been taken, if not the value of the index into the new array, if you have taken the self-subtraction key I 1 (The purpose is to repeat the loop until another index has not been fetched). Such a method of performance is very look character, reason to believe that see this kind of thinking of the students have understood.

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;
}

This is a pretty ingenious algorithm, after taking a random index in each loop, and remove its value from the array, so that if the index is still randomly fetched, the index is no longer the last value taken, and the range of random numbers decreases according to the length of the array. This can be a one-time cycle of a certain number of times to get the desired results.

You also see an improved version that takes into account some of the performance problems caused by the deletion of the array, the use of JK Big shuffle algorithm, that is, each delete operation changed to a position replacement operation (take the value of the index and the current self-subtraction key of the corresponding value of the exchange), so that the entire array of the effect 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;
}

Finally, a method of "creating a random array of values between Min~max" is given, and 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 help you with your JavaScript programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.