Created
December 21, 2015 09:53
-
-
Save taeguk/e41c93547e16af4ad4e5 to your computer and use it in GitHub Desktop.
Generate non-duplicate random numbers
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
void gen_rand_num_nondup(int *arr, int cnt, int s_num, int e_num) | |
{ | |
int range = e_num - s_num; | |
int in, im; | |
for (in = 0, im = 0; in < range && im < cnt; ++in) { | |
int rn = range - in; | |
int rm = cnt - im; | |
if (rand() % rn < rm) | |
arr[im++] = in + s_num; | |
} | |
assert(im == cnt); | |
} | |
//http://stackoverflow.com/questions/1608181/unique-random-numbers-in-an-integer-array-in-the-c-programming-language |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment