开发者

randomizing the string pulled from a plist file

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 mainBu开发者_StackOverflowndle] pathForResource:@"messages" ofType:@"plist"];
    NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];

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


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().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜