Skip to content

Instantly share code, notes, and snippets.

@chrismiles
Created June 6, 2011 07:05
Show Gist options
  • Save chrismiles/1009847 to your computer and use it in GitHub Desktop.
Save chrismiles/1009847 to your computer and use it in GitHub Desktop.
Random number on iOS
/* Return a random integer number between low and high inclusive */
int randomInt(int low, int high)
{
return (arc4random() % (high-low+1)) + low;
}
/* Return a random BOOL value */
BOOL randomBool()
{
return (BOOL)randomInt(0, 1);
}
/* Return a random float between 0.0 and 1.0 */
float randomClamp()
{
return (float)(arc4random() % ((unsigned)RAND_MAX + 1)) / (float)((unsigned)RAND_MAX + 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment