I need to pick a random index from those included in an NSIndexSet.
For reference, (It turns out -anyObject is not guaranteed to return a random object from a set.)NSSet defines the -anyObject method (documentation) for picking arbitrary objects from a set. Is there a similar functionality in NSIndexSet?
If not, how could it be implemented?
Note: I have found an implementation here, but it involves iteration over the index set's elements. Ideally I would like to avoid enumeration.
Edit: To my disappointment, the documentation of NSSet states that -anyObject is not guaranteed to return a random object from a set. Unfortunately, the same conclusion can be drawn from the NSIndexSet documentation, regarding the implementation of -getIndexes:maxCount:inIndexRange:
First generate a random number between
0and[indexSet count]-1.Now get the index at the
randomNumth index from the indexSet. There is noindexAtIndex:method as such, but this code will give you a similar result :Also have a look at this question.