|
|
Or, use your gamerDNA username: (more...)
| ||||||
| |
![]() |
| | LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
| | #1 (permalink) |
| King Poster Join Date: May 2002
Posts: 3,394
| 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) randomizeRows id)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?
__________________ I run a small forum for discussion of medical topics. |
| | |
| | #2 (permalink) |
| 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. |
| | |
| | #3 (permalink) |
| King Poster Join Date: May 2002
Posts: 3,394
| 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.
__________________ I run a small forum for discussion of medical topics. |
| | |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
| |