Created
March 9, 2012 21:05
-
-
Save guipn/2008699 to your computer and use it in GitHub Desktop.
Random integer within given range
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
#include <time.h> | |
unsigned int uirndrng(unsigned int low, unsigned int high) | |
{ | |
static char randomized; | |
if (low >= high) | |
{ | |
exit(EXIT_FAILURE); | |
} | |
if (randomized == 0) | |
{ | |
srand(time(NULL)); | |
randomized = 1; | |
} | |
// As suggested by c-faq.com: | |
return low + rand() / (RAND_MAX / (high - low + 1) + 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment