randomize array

Send Snippet To: Save this snippet to Code Collector Pro -- view all balzan's snippets
language: Objective-C
license: Other

Code for Snippet:

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];
comments powered by Disqus


Link to this snippet:

Download to Code Collector

To use the direct link to your snippet on CodeCollector.net either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 260
Date Added: 2016-07-26 13:52:57
Last Modified: 2016-07-26 07:53:11

Web Analytics