Skip to content

Instantly share code, notes, and snippets.

@priore
Created October 25, 2013 23:41
Show Gist options
  • Save priore/7163498 to your computer and use it in GitHub Desktop.
Save priore/7163498 to your computer and use it in GitHub Desktop.
Random numbers and random boolean values
@implementation NSNumber (Randomizer)
+ (NSNumber*)randomNumberWithMin:(NSInteger)min max:(NSInteger)max
{
if (min>max) {
int tempMax=max;
max=min;
min=tempMax;
}
int randomy=arc4random() % (max-min+1);
randomy=randomy+min;
return @(randomy);
}
+ (NSInteger)randomIntegerWithMin:(NSInteger*)min max:(NSInteger*)max
{
return [[NSNumber randomNumberWithMin:min max:max] integerValue];
}
+ (BOOL)randomBoolean
{
return [NSNumber randomIntegerWithMin:0 max:1] == 0;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment