//no callAsFunction: a stored block is
//the callable object
@interface Random : NSObject
@property (nonatomic, copy)
int (^next)(void);
- (instancetype)initWithLower:(int)lower
upper:(int)upper;
@end
@implementation Random
- (instancetype)initWithLower:(int)lower
upper:(int)upper {
self = [super init];
if (self) {
_next = ^int(void) {
return lower +
arc4random_uniform(
upper - lower + 1);
};
}
return self;
}
@end
Random *random = [[Random alloc]
initWithLower:1 upper:5];
int n1 = random.next();
int n2 = random.next();
//values in 1...5