Last active
July 23, 2016 09:16
-
-
Save kitt1987/de1e409b7be55d271ed177dfdf4c81cb to your computer and use it in GitHub Desktop.
Sampling uniformly
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
function randomSeq(begin, end, number) { | |
var i = 0; | |
var range = end - begin; | |
var exchanged = {}; | |
var seq = []; | |
while (i < number) { | |
const v = i + Math.floor(Math.random() * (range - i)); | |
if (exchanged[v] !== undefined) { | |
seq.push(begin + exchanged[v]); | |
} else { | |
seq.push(begin + v); | |
} | |
if (exchanged[i] !== undefined) { | |
exchanged[v] = exchanged[i]; | |
} else { | |
exchanged[v] = i; | |
} | |
++i; | |
} | |
return seq; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment