At the moment I am working on an iOS-application in which I need to draw random cards from a carddeck. At the moment my Code looks like this:
- (PlayingCard*) drawRandomCard
{
PlayingCard * randomCard = nil;
NSUInteger index = arc4random_uniform(self.cards.count);
randomCard = self.cards[index]; //self.cards is an NSArray of PlayingCard's
while (randomCard.isUsed) {
index = arc4random_uniform(self.cards.count);
randomCard = self.cards[index];
}
randomCard.used = YES;
return randomCard;
}
This methods gets called a lot (50'000 - 1'000'000 times for one monte-carlo-simulation)!
At the moment the whole thing is way to slow and I need really to optimize it.
I have some Ideas:
- faster random number generator
- change the whole high-level representation of the deck (Objective-C Class including an NSArray of PlayingCards) and cards (Objective-C Class) to a representation in normal C-Arrays and structs
- change the whole representation of the deck and cards to the bitwise level and do everything down there
What do you think?
Do you have any other ideas?
Do you know a better suited (faster) random number generator?
Thanks in advance!