Randomize Code Order
I have four blocks of code and I need to execute all four blocks but want to do so in a random order.
I was thinking of numbering the blocks 1-4 and then creating an array with the numbers in a random order and then cycling through the array and calling the code with a switch statement. Each block though should only execute once, so I would need to make sure the array had each integer only once.
Is there a faster/easier way to do this though?
I was thinking of numbering the blocks 1-4 and then creating an array with the numbers in a random order and then cycling through the array and calling the code with a switch statement. Each block though should only execute once, so I would need to make sure the array had each integer only once.
Is there a faster/easier way to do this though?
What language
Sir, e^iπ + 1 = 0, hence God exists; reply!
No, that's pretty much the easiest way to do it in any langauge, really. Unless you're programming in Python where you don't get a switch statement
Also, we recently had some discussion about shuffling a deck of cards or something so I'd look to that for ensuring that your array of ints in a random order only has each int once.
Or you could put all the blocks of code into functions, make an array of function pointers, randomize the array, and then iterate over said array once calling each function. That's probably the faster way.
Also, we recently had some discussion about shuffling a deck of cards or something so I'd look to that for ensuring that your array of ints in a random order only has each int once. Or you could put all the blocks of code into functions, make an array of function pointers, randomize the array, and then iterate over said array once calling each function. That's probably the faster way.
The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Code:
- (void)blockOne { ... }
- (void)blockTwo { ... }
- (void)blockThree { ... }
- (void)blockFour { ... }
- (void)performOperation
{
int i;
SEL actions[4] = {@selector(blockOne), @selector(blockTwo), @selector(blockThree), @selector(blockFour)};
int order[4] = {0, 1, 2, 3};
random_permutation(order, 4);
for(i = 0; i < 4; i++) [self performSelector:actions[0]];
}Sir, e^iπ + 1 = 0, hence God exists; reply!
How does this have anything to do with the random array "order"?
Looks like it'll perform blockOne each time.
*States that I don't have a good knowledge of SEL/selectors.
Code:
for(i = 0; i < 4; i++) [self performSelector:actions[0]];*States that I don't have a good knowledge of SEL/selectors.
I think that 0 should be an i...
also, random_permutation is a C++ function...
also, random_permutation is a C++ function...
Alright I got this working by building on the idea behind Unknown's code. I couldn't get the selector theory to work because I need to pass arguments to the code blocks.
Here is my working code, probably needs to be cleaned up a bit.
Note: oneDimensionalSetInt... is a function that just turns integers into numbers for the array and retrieves them...
Here is my working code, probably needs to be cleaned up a bit.
Note: oneDimensionalSetInt... is a function that just turns integers into numbers for the array and retrieves them...
Code:
NSMutableArray *order = [[NSMutableArray alloc] init];
for (i = 0; i < 4; i++) {
[order addObject:[NSNull null]];
[self oneDimensionalSetInt:i atIndex:i inArray:order];
}
//shuffle
int shuffleHolder, shuffleHolder1, randomPlace, randomPlace1;
for (i = 0; i < 5; i++) { // i'll shuffle 5 times...
randomPlace = (random() % 4);
randomPlace1 = (random() % 4);
shuffleHolder = [self oneDimensionalIntAtIndex:randomPlace inArray:order];
shuffleHolder1 = [self oneDimensionalIntAtIndex:randomPlace1 inArray:order];;
[self oneDimensionalSetInt:shuffleHolder atIndex:randomPlace1 inArray:order];
[self oneDimensionalSetInt:shuffleHolder1 atIndex:randomPlace inArray:order];
}
int orderValue;
for (i = 0; i < 4; i++) {
orderValue = [self oneDimensionalIntAtIndex:i inArray:order];
switch (orderValue) {
case 0:
[self blockOne];
break;
case 1:
[self blockTwo];
break;
case 2:
[self blockThree];
break;
case 3:
[self blockFour];
break;
default:
break;
}
}Code:
Sending messages
- performSelector:
- performSelector:withObject:
- performSelector:withObject:withObject:Sir, e^iπ + 1 = 0, hence God exists; reply!
unknown Wrote:Code:
Sending messages
- performSelector:
- performSelector:withObject:
- performSelector:withObject:withObject:
And if that's not enough arguments for you then you can use NSInvocation if you really have to.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| detecting 2 touches, is order of touch significant? | sefiroths | 4 | 9,376 |
Sep 30, 2011 07:29 AM Last Post: @iOS_blog |
|

