views:

34

answers:

1

I'm looking to randomize the output from a plist file. I've read about arc4random(), but I'm not sure how to incorporate it into the code.

thanks for any help.

here's the code that's currently pulling 'objectAtIndex:0'

 -(IBAction) buttonPress {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"messages" ofType:@"plist"];
    NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];

    [myMessage setText:[array objectAtIndex:0]];
    NSLog(@"%@",array);
 }
+2  A: 

The obvious thing to do is just use random():

[array objectAtIndex:random()%array.count]

arc4random() adds unnecessary complexity for little obvious benefit.

If you want values to be more random, you can call srandomdev() once (e.g. in main() or application:didFinishLaunchingWithOptions: or whatever) before calling random().

If you want "secure" random numbers, use SecRandomCopyBytes().

tc.
that worked perfectly, thank you!
hanumanDev