Last active
April 12, 2018 06:14
-
-
Save nyawach/bae6687da3fe9926d1d1babcb4770f99 to your computer and use it in GitHub Desktop.
Fisher-Yates法
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://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