Skip to content

Instantly share code, notes, and snippets.

@jaderfeijo
Last active March 1, 2016 16:39
Show Gist options
  • Select an option

  • Save jaderfeijo/ec360b18401cad2c3564 to your computer and use it in GitHub Desktop.

Select an option

Save jaderfeijo/ec360b18401cad2c3564 to your computer and use it in GitHub Desktop.
NSNumber+Random
//
// Created by Jader Feijo on 06/02/2014.
// Copyright (c) 2014 Jader Feijo. All rights reserved.
//
// @license MIT
//
#import <Foundation/Foundation.h>
@interface NSNumber (Random)
+ (float)randomFloat;
+ (float)randomFloatBetween:(float)min and:(float)max;
+ (NSInteger)randomInteger;
+ (NSInteger)randomIntegerBetween:(NSInteger)min and:(NSInteger)max;
@end
//
// Created by Jader Feijo on 06/02/2014.
// Copyright (c) 2014 Jader Feijo. All rights reserved.
//
// @license MIT
//
#import "NSNumber+Random.h"
@implementation NSNumber (Random)
+ (float)randomFloat {
return (float)arc4random() / UINT_MAX;
}
+ (float)randomFloatBetween:(float)min and:(float)max {
return min + (max - min) * [NSNumber randomFloat];
}
+ (NSInteger)randomInteger {
return (NSInteger)arc4random();
}
+ (NSInteger)randomIntegerBetween:(NSInteger)min and:(NSInteger)max {
return min + arc4random() % (max - min);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment