« 2014年08月 | メイン | 2014年10月 »

2014年09月22日

javascriptランダム化

とりあえず何も考えずにw


function shuffle(array) {
var copy = [], n = array.length, i;

// While there remain elements to shuffle…
while (n) {

// Pick a remaining element…
i = Math.floor(Math.random() * array.length);

// If not already shuffled, move it to the new array.
if (i in array) {
copy.push(array[i]);
delete array[i];
n--;
}
}

return copy;
}

投稿者 muuming : 11:16