@Darryl’s answer is correct, but could use some minor tweaks:
NSUInteger randomIndex = arc4random() % theArray.count;
Modifications:
- Using
arc4random()
overrand()
andrandom()
is simpler because it does not require seeding (callingsrand()
orsrandom()
). - The modulo operator (
%
) makes the overall statement shorter, while also making it semantically clearer.