Skip to content

Instantly share code, notes, and snippets.

@kitt1987
Last active July 23, 2016 09:16
Show Gist options
  • Save kitt1987/de1e409b7be55d271ed177dfdf4c81cb to your computer and use it in GitHub Desktop.
Save kitt1987/de1e409b7be55d271ed177dfdf4c81cb to your computer and use it in GitHub Desktop.
Sampling uniformly
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