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. - int main()
-
{
-
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
-
NSMutableArray *myColors;
-
-
myColors = [NSMutableArray arrayWithObjects: @"Red", @"Red",@"Red", @"Red", @"Red", @"Green", @"Green", @"Green", @"Blue", @"Blue", @"Blue", @"Yellow", nil];
-
-
-
srandom(time(NULL));
-
NSUInteger count = [myColors count];
-
for (NSUInteger i = 0; i < count; ++i) {
-
int nElements = count - i;
-
int n = (random() % nElements) + i;
-
[myColors exchangeObjectAtIndex:i withObjectAtIndex:n];
-
NSLog (@"Element %i = %@", i, [myColors objectAtIndex: i]);
-
}
-
-
[pool drain]; return 0;
-
}
-
Which outputs something like: - Element 0 = Blue
-
Element 1 = Green
-
Element 2 = Yellow
-
Element 3 = Blue
-
Element 4 = Green
-
Element 5 = Red
-
Element 6 = Red
-
Element 7 = Red
-
Element 8 = Blue
-
Element 9 = Green
-
Element 10 = Red
-
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: - Red, Green, Red, Blue, Red, Green, Yellow, Red, Blue, Red, Green, Blue
-
Any help or suggestions would be great.
0 1230 Sign in to post your reply or Sign up for a free account.
Similar topics
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',
),...
|
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);
}
|
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...
|
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...
|
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...
|
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>
|
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...
|
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),
-...
|
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(...
|
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
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| | |