/Classes/RNG.m
Objective C | 73 lines | 53 code | 15 blank | 5 comment | 5 complexity | 654a0e4813b893c5e7ccc31f3894e9c4 MD5 | raw file
Possible License(s): Apache-2.0
1// 2// RNG.m 3// 4// Created by Karl Stenerud on 10-02-15. 5// 6 7#import "RNG.h" 8 9SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(RNG); 10 11 12@implementation RNG 13 14SYNTHESIZE_SINGLETON_FOR_CLASS(RNG); 15 16- (id) init 17{ 18 return [self initWithSeed:time(NULL)]; 19} 20 21- (id) initWithSeed:(unsigned int) seedValueIn 22{ 23 if(nil != (self = [super init])) 24 { 25 self.seedValue = seedValueIn; 26 } 27 return self; 28} 29 30@synthesize seedValue; 31 32- (void) setSeedValue:(unsigned int) value 33{ 34 seedValue = value; 35 srand(seedValue); 36} 37 38- (unsigned int) randomUnsignedInt 39{ 40 return rand(); 41} 42 43- (double) randomProbability 44{ 45 return rand() / 2147483647.0; 46} 47 48- (int) randomNumberFrom: (int) minValue to: (int) maxValue 49{ 50 double probability = rand() / 2147483648.0; 51 double range = maxValue - minValue + 1; 52 return (int)(range * probability + minValue); 53} 54 55- (int) randomNumberFrom: (int) minValue to: (int) maxValue except:(int) exceptValue 56{ 57 if(minValue == maxValue) 58 { 59 return minValue; 60 } 61 int result; 62 while(exceptValue == (result = [self randomNumberFrom:minValue to:maxValue])) 63 { 64 } 65 return result; 66} 67 68- (bool) randomBool 69{ 70 return rand() & 1; 71} 72 73@end