从 NSArray 中随机选择一个 objectAtIndex

发布于 2024-12-12 10:50:56 字数 141 浏览 0 评论 0原文

我有一个 NSArray,其中包含索引 0 - 9 的 10 个对象。数组中的每个条目都是一个引号。

当我的用户选择“随机引用”选项时,我希望能够从数组中选择一个随机条目并显示该条目中包含的文本。

谁能指出我如何实现这一目标的正确方向?

I have an NSArray which contains 10 objects from index 0 - 9. Each entry in the array is a quotation.

When my user selects the 'random quote' option I want to be able to select a random entry from the array and display the text that is contained in that entry.

Can anyone point me in the right direction on how to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

堇年纸鸢 2024-12-19 10:50:56

我建议您使用它而不是硬编码 10;这样,如果您添加更多报价,它会自动计算出来,而无需更改该数字。

NSInteger randomIndex = arc4random()%[array count];
NSString *quote = [array objectAtIndex:randomIndex];

I'd recommend you use this instead of hardcoding the 10; that way, if you add more quotations, it will work it out automatically, without you needing to change that number.

NSInteger randomIndex = arc4random()%[array count];
NSString *quote = [array objectAtIndex:randomIndex];
也只是曾经 2024-12-19 10:50:56

您可能想要使用 arc4random() 从 0-9 中选择一个对象。然后,只需

NSString* string = [array objectAtIndex:randomPicked];

获取条目的文本即可。

Your probably going to want to use arc4random() to pick an object from 0-9. Then, simply do

NSString* string = [array objectAtIndex:randomPicked];

to get the text of the entry.

溺渁∝ 2024-12-19 10:50:56

您可以使用arc4random()%10来获取索引。存在轻微的偏差,这应该不是问题。

最好使用arc4random_uniform(10),没有偏差,而且更容易使用。

You can use arc4random()%10 to get an index. There is a slight bias that should not be a problem.

Better yet use arc4random_uniform(10), there is no bias and is even easier to use.

你是暖光i 2024-12-19 10:50:56

首先,在边界之间获取一个随机数,请参阅 此讨论 和相关的手册页。然后用它索引到数组中。

int random_number = rand()%10; // Or whatever other method.
return [quote_array objectAtIndex:random_number];

编辑:对于那些无法从链接正确插入或只是不想阅读建议参考文献的人,让我为您说明一下:

// Somewhere it'll be included when necessary, 
// probably the header for whatever uses it most.
#ifdef DEBUG 
#define RAND(N) (rand()%N)
#else 
#define RAND(N) (arc4random()%N)
#endif

...

// Somewhere it'll be called before RAND(), 
// either appDidLaunch:... in your application delegate 
// or the init method of whatever needs it.
#ifdef DEBUG
// Use the same seed for debugging 
// or you can get errors you have a hard time reproducing.
srand(42);
#else
// Don't seed arc4random()
#endif

....

// Wherever you need this.
NSString *getRandomString(NSArray *array) {
    #ifdef DEBUG
    // I highly suggest this, 
    // but if you don't want to put it in you don't have to.
    assert(array != nil);
    #endif
    int index = RAND([array count]);
    return [array objectAtIndex:index];
}

First, get a random number between your bounds, see this discussion and the relevant man pages. Then just index into the array with it.

int random_number = rand()%10; // Or whatever other method.
return [quote_array objectAtIndex:random_number];

Edit: for those who can't properly interpolate from the links or just don't care to read suggested references, let me spell this out for you:

// Somewhere it'll be included when necessary, 
// probably the header for whatever uses it most.
#ifdef DEBUG 
#define RAND(N) (rand()%N)
#else 
#define RAND(N) (arc4random()%N)
#endif

...

// Somewhere it'll be called before RAND(), 
// either appDidLaunch:... in your application delegate 
// or the init method of whatever needs it.
#ifdef DEBUG
// Use the same seed for debugging 
// or you can get errors you have a hard time reproducing.
srand(42);
#else
// Don't seed arc4random()
#endif

....

// Wherever you need this.
NSString *getRandomString(NSArray *array) {
    #ifdef DEBUG
    // I highly suggest this, 
    // but if you don't want to put it in you don't have to.
    assert(array != nil);
    #endif
    int index = RAND([array count]);
    return [array objectAtIndex:index];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文