Skip to content

Instantly share code, notes, and snippets.

@charlieelliott
Created July 23, 2014 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlieelliott/e2a12316bebb9f3b5a9a to your computer and use it in GitHub Desktop.
Save charlieelliott/e2a12316bebb9f3b5a9a to your computer and use it in GitHub Desktop.
giveSomeGifts
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