Fires of Heaven Guild Message Board  

Go Back   Fires of Heaven Guild Message Board > General forums > Development
User Name
Password
ForumSpy Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 04-15-2006, 04:55 PM   #1 (permalink)
aychamo_aycono
Legendary
 
Join Date: May 2002
Posts: 2,915
Randomizing rows

Hey there

Ok, I have a table with a lot of rows. I want to randomize my rows. I'm using CoreData for my database. The best thing I've come up with is to add an atribute in my codedata model for the random number, and go through and give each row a random number and then sort based on that random number. here is the code I have for giving each row it's random number:

- (IBAction) randomizeRowsid)sender {


int maximumSelection = [[imageArray valueForKeyPath:@"arrangedObjects.@count"] intValue];

BOOL randomArray[maximumSelection];
BOOL arrayIsFull = NO;
int randomIndex = 0;
int count = 0;

for (count = 0; count < maximumSelection; count++) {
randomArray[count] = NO;
}

while (arrayIsFull == NO) {

randomIndex = (random() % maximumSelection);

if (randomArray[randomIndex] == NO) {
randomArray[randomIndex] = YES;
[imageArray setSelectionIndex: randomIndex];
[imageArray setValue: [NSNumber numberWithInt:randomIndex] forKeyPath:@"selection.random"];
}


int yesCount = 0;
for (count = 0; count < maximumSelection; count++) {
if (randomArray[count] == YES) {yesCount++;}

}

if (yesCount == count) {arrayIsFull = YES;}

}
[self saveAction: self];

}

Is this a really stupid way of doing it? It works fine, but seems perversely inelegant. Any suggestions?
__________________
Lumie: There are no cancers of the heart.
Aychamo: http://www.emedicine.com/med/topic280.htm
aychamo_aycono is offline   Reply With Quote
Old 04-17-2006, 03:55 PM   #2 (permalink)
Fattyfat
Administrator
 
Join Date: Dec 2004
Location: ftw tx
Posts: 293
+4 Internets
Well a lot of that depends on the db you are using. Just by looking at whatever this is, it doesn't look like your database uses SQL, because if it did, you could just pull the rows out of your table randomly using SQL rather than actually change the physical state of your database or use some SQL statement to randomize your rows in the db...

otherwise, that seems like a fine operation. Its going to take you O(n) to create the randomness and then O(sort_method) to sort the data. Just make sure your randomizer gets a different seed for its random number generation... if that's even a problem.
Fattyfat is offline   Reply With Quote
Old 04-18-2006, 02:44 PM   #3 (permalink)
aychamo_aycono
Legendary
 
Join Date: May 2002
Posts: 2,915
Hey man!

It uses CoreData. It's some new technology Apple put in Cocoa. It makes database apps so extremely mega easy.

Thank you for the comments about the routine, I just wanted to make sure it wasn't retarded.
__________________
Lumie: There are no cancers of the heart.
Aychamo: http://www.emedicine.com/med/topic280.htm
aychamo_aycono is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
uberguilds network



All times are GMT -7. The time now is 09:41 PM.


Powered by vBulletin® Version 3.6.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0 RC6