Skip to content

Instantly share code, notes, and snippets.

@nyawach
Last active April 12, 2018 06:14
Show Gist options
  • Save nyawach/bae6687da3fe9926d1d1babcb4770f99 to your computer and use it in GitHub Desktop.
Save nyawach/bae6687da3fe9926d1d1babcb4770f99 to your computer and use it in GitHub Desktop.
Fisher-Yates法
// http://qiita.com/nyawach/items/108c8e2551c26674f57b
function combination(n, m) {
var list = [];
m = (m == null) ? n : m;
for(var i=n-1; i--;) list.push(i);
for(var i=list.length,j;--i;){
j = Math.random()*(i+1) | 0;
list[i] = [list[j], list[j] = list[i]][0];
}
return list.slice(-m);
}
// for ES6
function generateRandomArray(num, limit=num){
const list = [...Array(num)].map((_, i)=> i);
for(let i=list.length,j;--i;){
j = Math.random()*(i+1) | 0;
[list[i], list[j]] = [list[j], list[i]];
}
return list.slice(-limit);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment