简体   繁体   English

随机化从plist文件中提取的字符串

[英]randomizing the string pulled from a plist file

I'm looking to randomize the output from a plist file. 我正在寻找随机化plist文件的输出。 I've read about arc4random(), but I'm not sure how to incorporate it into the code. 我已经读过关于arc4random()的文章,但不确定如何将其合并到代码中。

thanks for any help. 谢谢你的帮助。

here's the code that's currently pulling 'objectAtIndex:0' 这是当前提取“ 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);
 }

The obvious thing to do is just use random(): 显而易见的事情就是使用random():

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

arc4random() adds unnecessary complexity for little obvious benefit. arc4random()增加了不必要的复杂性,几乎没有什么好处。

If you want values to be more random, you can call srandomdev() once (eg in main() or application:didFinishLaunchingWithOptions: or whatever) before calling random(). 如果您希望值更加随机,则可以在调用random()之前调用srandomdev()一次(例如,在main()或application:didFinishLaunchingWithOptions:或其他任何方式中)。

If you want "secure" random numbers, use SecRandomCopyBytes(). 如果要“安全”随机数,请使用SecRandomCopyBytes()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM