Last active
November 16, 2016 08:11
-
-
Save hoosin/5f360c7f4f48f2046e556f4f32195555 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 洗牌算法: | |
* 1. 从第一张牌开始,将每张牌和随机的一张牌进行交换 | |
* 2. 更优解法:从第一张牌开始,将每张牌和之前全部牌中随机的一张进行交换 | |
* 3. C++ 标准库函数 std::random_shuffle (原理与第一点差不多) | |
*/ | |
//inspired by http://www.cs.princeton.edu/~rs/ | |
function shuffle(arr) { | |
var len = arr.length, i; | |
while (len) { | |
i = Math.random() * len-- | 0; // 0 ≤ i < n | |
arr[len] = [ arr[i], arr[i] = arr[len] ][0]; | |
} | |
return arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment