1

I have 4 void statements and I want to randomize them so one out of the 4 triggers at a time. Like for example the first void triggers then the next time maybe the third void triggers and so fourth. Could I use arc4random() or do I need another approach?

3 Answers3

3

Sure you can use arc4Random. (It's better to use arc4random_uniform, as @JustSid pointed out in his comments. Off to fix my sample code...) There are a nearly infinite number of ways to do this.

First, a gripe of mine. Don't call methods "voids". That's inaccurate and misleading. (And it makes you sound ignorant about programming.) They're methods. The text inside the parenthesis at the beginning of the method tells you what kind of value it returns. If it doesn't return anything, the word "void" is C language notation for "nothing."

So the method:

-(void) foo;

Takes no parameters and doesn't return anything, where the method:

-(BOOL) bar;

...also takes no parameters, but it returns a boolean result.

The first method is not a "void". It is a method that doesn't return a result.

Now, to your question:

You could do something like this:

- (void) foo;
{
  NSLog(@"foo");
}

- (void) bar;
{
  NSLog(@"bar");
}

- (void) foobar;
{
  NSLog(@"foobar");
}

- (void) randomMethod;
{
  int index = arc4random_uniform(3); 
  switch (index)
  {
    case 0:
      [self foo];
      break;
    case 1:
      [self bar];
      break;
    case 2:
      [self foobar];
      break;
   }
}

You could also use blocks. You could set up an array of block pointers, use arc4random_uniform() to pick an array index, and execute the appropriate block from the array. (Blocks are objects so you can add them to an array.)

The syntax of blocks and block pointers is a little tricky to follow, so for simplicity I'm not going to write that out. If you're interested I can amend my answer to show how that's done.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • One gripe: saying that `void` is a C-ism for nothing tends to confuse people later on when they need to use void pointers. – SevenBits Feb 07 '15 at 18:08
  • True enough. Void pointers are a horse of a different color, and those are `void *`. (A syntax I never liked. I'd prefer something like `unknown *`) But then nobody ever said C was pretty, or easy to read. My father-in-law says "C is a write-only language." – Duncan C Feb 07 '15 at 18:14
  • Although this was clearly done to save on keywords, the reuse of `void` in this fashion serves only to confuse. But all C programmers gotta stomach it, I suppose... – SevenBits Feb 07 '15 at 18:17
0

arc4random() is perfect for that.

int a = arc4random() % 4;
switch (a) {
    case 0:
        [self void0];
        break;
   case 1:
        [self void1];
        break;
    // ...
}
return true
  • 7,839
  • 2
  • 19
  • 35
0

For a scalable solution, you can use NSSelectorFromString.
The NSSelectorFromString will generate this warning: "performSelector may cause a leak because its selector is unknown". If you can't live with the warning, there are solution for that.

NSArray *methods = @[@"method1", @"method2", @"method3", @"method4"];  //add more if needed

int index = arc4random_uniform((int)methods.count);
NSString *selectedMethod = [methods objectAtIndex:index];

SEL s = NSSelectorFromString(selectedMethod);
[self performSelector:s];

-(void)method1
{
    NSLog(@"method1 is called");
}

-(void)method2
{
    NSLog(@"method2 is called");
}

-(void)method3
{
    NSLog(@"method3 is called");
}
-(void)method4
{
    NSLog(@"method4 is called");
}
Community
  • 1
  • 1
user523234
  • 14,323
  • 10
  • 62
  • 102