Created
July 23, 2014 02:01
-
-
Save charlieelliott/e2a12316bebb9f3b5a9a to your computer and use it in GitHub Desktop.
giveSomeGifts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void giveGifts(int iteration) { | |
NSArray *people = @[@"a", @"b", @"c", @"d", @"e"]; | |
NSMutableArray *given = [NSMutableArray array]; | |
NSMutableArray *received = [NSMutableArray array]; | |
int count = 0; //we don't accidentally want an infinate loop | |
while (people.count != given.count && people.count != received.count && count < 2000) { | |
int a = arc4random_uniform((int32_t)people.count); | |
int b = arc4random_uniform((int32_t)people.count); | |
BOOL aHasGiven = [given containsObject:people[a]]; | |
BOOL bHasReceived = [received containsObject:people[b]]; | |
BOOL bHasGiven = [given containsObject:people[b]]; | |
if(a != b) | |
{ | |
if(!aHasGiven && ((!bHasReceived && !bHasGiven) || (!bHasReceived && given.count < people.count)) ) | |
{ | |
[given addObject:people[a]]; | |
[received addObject:people[b]]; | |
} | |
} | |
count++; | |
} | |
NSLog(@"\niteration: %d\ncount: %d \ng:%@ \nr:%@", iteration, count, given, received); | |
} | |
void giveSomeGifts() | |
{ | |
for(int i = 0; i < 10; i++) | |
giveGifts(i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment