Created
July 20, 2011 23:49
-
-
Save sakunyo/1096201 to your computer and use it in GitHub Desktop.
js Fisher-Yates shuffle
This file contains hidden or 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
// ランダム並び替えの方法が間違ってたのでどれほど間違ってたのかを可視化してみた - さらさら宇宙忍法帖 | |
// http://d.hatena.ne.jp/gotin/20071211/shuffle | |
function shuffle_fy(array){ | |
var n = array.length; | |
while (--n) { | |
var k = Math.floor(Math.random() * n); // 0 <= k <= n (!) | |
var temp = array[n]; | |
array[n] = array[k]; | |
array[k] = temp; | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment