Skip to content

Instantly share code, notes, and snippets.

@christianbundy
Last active December 21, 2015 09:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianbundy/6287955 to your computer and use it in GitHub Desktop.
Save christianbundy/6287955 to your computer and use it in GitHub Desktop.
Generates a set of random unique integers.
// do not use this
function random_uniques(min, max, num) {
if (max-min > num) {
_result = {};
for (i = 1; i<=num; i++) {
_unique=false;
while (!_unique) {
_new = Math.round((Math.random()*max)+min);
for (j = 1; j<=i; j++) {
_unique = true;
if (_result[j] === _new) {
_unique = false;
}
}
}
_result[i] = _new;
}
return _result;
} else {
return false;
}
}
random_uniques(1,100,5); // Object {1: 99, 2: 86, 3: 37, 4: 55, 5: 7}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment