I'm using NSArray/NSMutable array to store different objects. Once all the objects are added, I want to re-arrange them in random order. How can I do it?
NSUInteger count = [yourMutableArray count]; for (NSUInteger i = 0; i < count; ++i) { // Select a random element between i and end of array to swap with. int nElements = count - i; int n = (arc4random() % nElements) + i; [yourMutableArray exchangeObjectAtIndex:i withObjectAtIndex:n]; }
2.1m questions
2.1m answers
60 comments
57.0k users