SHARE
TWEET

Random numbers and random boolean values

priore Jun 14th, 2013 809 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @implementation NSNumber (Randomizer)
  2.  
  3. + (NSNumber*)randomNumberWithMin:(NSInteger)min max:(NSInteger)max
  4. {
  5.     if (min>max) {
  6.         NSInteger tempMax=max;
  7.         max=min;
  8.         min=tempMax;
  9.     }
  10.     NSInteger randomy=arc4random() % (max-min+1);
  11.     randomy=randomy+min;
  12.     return @(randomy);
  13. }
  14.  
  15. + (NSInteger)randomIntegerWithMin:(NSInteger)min max:(NSInteger)max
  16. {
  17.     return [[NSNumber randomNumberWithMin:min max:max] integerValue];
  18. }
  19.  
  20. + (BOOL)randomBoolean
  21. {
  22.     return [NSNumber randomIntegerWithMin:0 max:1] == 0;
  23. }
  24.  
  25. @end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top