Possible Duplicate:
iphone - nsarray/nsmutablearray - re-arrange in random order
I have an NSMutableArray that contains 20 objects. Is there any way that I could randomize their order like when you shuffle a deck. (By order I mean their index in the array)
Like if I had an array that contained:
How could I randomize the order so that I could get something like:
Here's some sample code: Iterates through the array, and randomly switches an object's position with another.
for (int x = 0; x < [array count]; x++) {
int randInt = (arc4random() % ([array count] - x)) + x;
[array exchangeObjectAtIndex:x withObjectAtIndex:randInt];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With