473,411 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Objective-C, "Shuffling" an NSMutuable Array

I'm not sure, if this is best placed here or in the math/algorithms section.

I'm trying to sort an array so it is as low discrepancy as possible.

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  4.     NSMutableArray *myColors;
  5.  
  6.     myColors = [NSMutableArray arrayWithObjects: @"Red", @"Red",@"Red", @"Red", @"Red", @"Green", @"Green", @"Green", @"Blue", @"Blue", @"Blue", @"Yellow", nil];
  7.  
  8.  
  9.     srandom(time(NULL));
  10.     NSUInteger count = [myColors count];
  11.     for (NSUInteger i = 0; i < count; ++i) {
  12.         int nElements = count - i;
  13.         int n = (random() % nElements) + i;
  14.         [myColors exchangeObjectAtIndex:i withObjectAtIndex:n];
  15.          NSLog (@"Element %i = %@", i, [myColors objectAtIndex: i]);
  16.     }
  17.  
  18. [pool drain]; return 0;
  19. }
  20.  
Which outputs something like:

Expand|Select|Wrap|Line Numbers
  1. Element 0 = Blue
  2.      Element 1 = Green
  3.      Element 2 = Yellow
  4.      Element 3 = Blue
  5.      Element 4 = Green
  6.      Element 5 = Red
  7.      Element 6 = Red
  8.      Element 7 = Red
  9.      Element 8 = Blue
  10.      Element 9 = Green
  11.      Element 10 = Red 
  12.      Element 11 = Red
Which shuffles the array, but it isn't as low discrepancy as I would like mainly due to random number seed.

Ideally each instance should be as far away from another one of it's kind as possible like:

Expand|Select|Wrap|Line Numbers
  1. Red, Green, Red, Blue, Red, Green, Yellow, Red, Blue, Red, Green, Blue
  2.  
Any help or suggestions would be great.
Jul 28 '10 #1
0 1230

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Reed Law | last post by:
I have the exact same data in two arrays, but only the array created like so will work: $spaw_imglibs = array( array( 'value' => '/youth/pics/Member pics/', 'text' => 'Member pics', ),...
2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
0
by: sjaak538 | last post by:
Hello I've a question about printing complex array's with print_r $xmlC->obj_data I get this example bellow But can anybody give me one example on how to echo $xmlC->obj_data; I try'd with...
3
by: Paul Kirby | last post by:
Hello All I am trying to update me code to use arrays to store a group of information and I have come up with a problem sorting the multiple array :( Array trying to sort: (7 arrays put into...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
5
by: Nico | last post by:
Hello folks, I am currently storing a set of objects inside an array, $itemlist = array(); $itemlist = new item("myitem"); //... and I am looking to develop a search function, which...
2
by: maciej | last post by:
I got an array that consists of elements that are arrays also. Now I wish I could add an element to the middle of it. Let mi give you an example: array ( - array(1,15,apple), -...
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.