Created
December 6, 2016 10:59
-
-
Save tianyk/881e3db3d69ef5612d9d039f3eaaca7d to your computer and use it in GitHub Desktop.
This file contains 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
; | |
/** | |
* 要求,种子相同的情况下返回的随机序列数组相同。 | |
* e.g. | |
* <= (1477044467806, 4) | |
* => [3, 2, 0, 1] | |
* | |
* <= (1477044467886, 6) | |
* => [3, 4, 2, 5, 0, 1] | |
* @param seed 随机数种子 | |
* @param length 序列长度 | |
*/ | |
function shuffle(length, seed) { | |
if (!seed) seed = Date.now(); | |
var sort = []; | |
var temp, j; | |
for (var i = 0; i < length; i++) { | |
j = (seed % (i + 1) + i) % length; | |
temp = sort[i] || i; | |
sort[i] = sort[j] || j; | |
sort[j] = temp; | |
} | |
return sort; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment