/Classes/RNG.m
http://github.com/kstenerud/ObjectAL-for-iPhone · Objective C · 73 lines · 53 code · 15 blank · 5 comment · 5 complexity · 654a0e4813b893c5e7ccc31f3894e9c4 MD5 · raw file
- //
- // RNG.m
- //
- // Created by Karl Stenerud on 10-02-15.
- //
- #import "RNG.h"
- SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(RNG);
- @implementation RNG
- SYNTHESIZE_SINGLETON_FOR_CLASS(RNG);
- - (id) init
- {
- return [self initWithSeed:time(NULL)];
- }
- - (id) initWithSeed:(unsigned int) seedValueIn
- {
- if(nil != (self = [super init]))
- {
- self.seedValue = seedValueIn;
- }
- return self;
- }
- @synthesize seedValue;
- - (void) setSeedValue:(unsigned int) value
- {
- seedValue = value;
- srand(seedValue);
- }
- - (unsigned int) randomUnsignedInt
- {
- return rand();
- }
- - (double) randomProbability
- {
- return rand() / 2147483647.0;
- }
- - (int) randomNumberFrom: (int) minValue to: (int) maxValue
- {
- double probability = rand() / 2147483648.0;
- double range = maxValue - minValue + 1;
- return (int)(range * probability + minValue);
- }
- - (int) randomNumberFrom: (int) minValue to: (int) maxValue except:(int) exceptValue
- {
- if(minValue == maxValue)
- {
- return minValue;
- }
- int result;
- while(exceptValue == (result = [self randomNumberFrom:minValue to:maxValue]))
- {
- }
- return result;
- }
- - (bool) randomBool
- {
- return rand() & 1;
- }
- @end