Randomize Code Order

ChrisPol
Unregistered
 
Post: #1
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?
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
What language

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
ChrisPol
Unregistered
 
Post: #3
whoops!

Objective-C
Quote this message in a reply
Member
Posts: 257
Joined: 2004.06
Post: #4
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 Smile 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).
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #5
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!
Quote this message in a reply
Member
Posts: 312
Joined: 2006.10
Post: #6
How does this have anything to do with the random array "order"?
Code:
for(i = 0; i < 4; i++) [self performSelector:actions[0]];
Looks like it'll perform blockOne each time.

*States that I don't have a good knowledge of SEL/selectors.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #7
I think that 0 should be an i...

also, random_permutation is a C++ function...
Quote this message in a reply
ChrisPol
Unregistered
 
Post: #8
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...

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;
        }    
    }
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #9
Code:
Sending messages

                - performSelector:
                - performSelector:withObject:
                - performSelector:withObject:withObject:

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #10
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  detecting 2 touches, is order of touch significant? sefiroths 4 10,056 Sep 30, 2011 07:29 AM
Last Post: @iOS_blog