Last active
December 21, 2015 09:49
-
-
Save christianbundy/6287955 to your computer and use it in GitHub Desktop.
Generates a set of random unique integers.
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
// 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