Skip to content

Instantly share code, notes, and snippets.

@Helios-vmg
Created August 28, 2015 15:22
Show Gist options
  • Save Helios-vmg/38a5321067c377081140 to your computer and use it in GitHub Desktop.
Save Helios-vmg/38a5321067c377081140 to your computer and use it in GitHub Desktop.
//Returns an integer in [0;max], where max <= RAND_MAX
int unbiased_rand(int max){
const int rand_max = RAND_MAX - (RAND_MAX + 1) % (max + 1);
int r;
do
r = rand();
while (r > rand_max);
return r % (max + 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment