JavaScript randomly breaks the position of array elements (shuffling algorithm)

Source: Internet
Author: User
[Javascript] functionmess (arr) {var_floorMath.floor, _ randomMath. random, lenarr. length, I, j, arri, n_floor (len/2) + 1; while (n --) {I _floor (_ random () * len );...

[Javascript]
Function mess (arr ){
Var _ floor = Math. floor, _ random = Math. random,
Len = arr. length, I, j, arri,
N = _ floor (len/2) + 1;
While (n --){
I = _ floor (_ random () * len );
J = _ floor (_ random () * len );
If (I! = J ){
Arri = arr [I];
Arr [I] = arr [j];
Arr [j] = arri;
}
}
Return arr;
}
Var testa = [1, 2, 3, 4, 5, 6, 7];
Mess (testa );
Console. log (testa );
Note that this is to change the original array. If you do not want to change the original array, copy the original array first:

[Javascript]
Var testa = [1, 2, 3, 4, 5, 6, 7];
Var newarr = mess (testa. slice (0); // here it is just a shallow copy Array
Console. log (testa );
Console. log (newarr );
Is this shuffling function good?
Haha, maybe after testing, you will find that some elements are still in the original position after the cards are washed, and there is a high probability of such elements. How can we improve it?

In reality, after we finish playing cards, we usually split poker into two parts (commonly known as "card cutting"), and then swap the upper and lower positions of the two parts. Well, we didn't switch cards in this shuffling function!

Therefore, we can make the following improvements:

[Javascript]
// Shuffling Algorithm
Function mess (arr ){
Var _ floor = Math. floor, _ random = Math. random,
Len = arr. length, I, j, arri,
N = _ floor (len/2) + 1;
While (n --){
I = _ floor (_ random () * len );
J = _ floor (_ random () * len );
If (I! = J ){
Arri = arr [I];
Arr [I] = arr [j];
Arr [j] = arri;
}
}
// Add a card switch operation
I = _ floor (_ random () * len );
Arr. push. apply (arr, arr. splice (0, I ));
// Return arr; // do you want to return the disrupted array?
}
Var testa = [1, 2, 3, 4, 5, 6, 7, 8, 9];
Var newarr = testa. slice (0 );
Mess (newarr );
Console. log (testa );
Console. log (newarr );
Well, now it seems that there are very few elements that remain unchanged before and after shuffling. It can be said that success has been achieved.
Bart, wait a moment. We have found that this shuffling function will change the original array. So, the next question is: do you want this function to return a value and return the disrupted array? Because I may want to call it like this:
[Javascript]
Var testa = [1, 2, 3, 4, 5, 6, 7];
Var newarr = mess (testa. slice (0); // here it is just a shallow copy Array
Is this call method good? If this call method is added, there are two call methods.
In fact, for such a simple function, the simpler the calling method, the better, and the easier it is to use. It is best to provide only one call method. When there are many ways to use it, you need to remember more things, which is easy to confuse. Therefore, I don't want to return values for this shuffling function, and I don't want to provide this call method.
There is another reason: this is conducive to emphasizing that this is a function that changes the original array, rather than providing the call result through the return value, so that the caller can clearly understand the impact of the function.

 

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.