Objective-C Tips & Trick - Prioregroup.com
[Objective-C] Random numbers and random boolean values
       
back

@implementation NSNumber (Randomizer)

+ (NSNumber*)randomNumberWithMin:(NSInteger)min max:(NSInteger)max
{
    if (min>max) {
        NSInteger tempMax=max;
        max=min;
        min=tempMax;
    }
    NSInteger 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






Copyright © 1996-2018 Centro Studi Informatica di Danilo Priore. All rights reserved. P.I.10149810581.
prioregroup.com