Skip to content

Instantly share code, notes, and snippets.

@sakunyo
Created July 20, 2011 23:49
Show Gist options
  • Save sakunyo/1096201 to your computer and use it in GitHub Desktop.
Save sakunyo/1096201 to your computer and use it in GitHub Desktop.
js Fisher-Yates shuffle
// ランダム並び替えの方法が間違ってたのでどれほど間違ってたのかを可視化してみた - さらさら宇宙忍法帖
// 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