iOS版 - 如何选择数组对象更多,所以后来其他 [英] iOS - How To Choose Objects in Array More-So Then Others

查看:199
本文介绍了iOS版 - 如何选择数组对象更多,所以后来其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含各种字符串数组。我想选择在指标3,7&放一个对象; 9点多,所以当时所有其他objects..how我能做到这一点。 (比方说,我每次抢阵列中的一个对象的时候,我希望这是一个80%的机会,在指数3,7或9对象被选中)

I have an array that contains a variety of strings. I want to choose a the objects at indexes 3,7 & 9 more-so then all the other objects..how can I achieve this. (Let's say every time I grab an object within the array, I want there to be a 80% chance that object at index 3,7 or 9 is selected)

下面是我的数组:

 NSMutableArray* animalImagesArray = [[NSMutableArray alloc] initWithObjects:@"bull.png",@"bunny.png",@"chicken.png",@"cow.png",@"crocodile.png",@"dog.png",@"donkey.png",@"duck.png",@"elephant.png",@"giraffe.png",@"goat.png",@"hippo.png",@"horse.png",@"lion.png",@"monkey.png",@"parrot.png",@"pig.png",@"rooster.png",@"sheep.png",@"snake.png",@"tiger.png",@"warthog.png",@"zebra.png", nil];

任何帮助将大大AP preciated。在此先感谢!

Any help would be greatly appreciated. Thanks in advance!

推荐答案

我喜欢Lyndsey斯科特的回答让想我会实现它,这样你仍然可以得到这个词的原始索引。

I like Lyndsey Scott's answer so thought I'd implement it so that you can still get the original index of the word.

如果你有10个项目的数组,你想他们中的3(同样相结合)有被选中的机率为80%那么这意味着其他7有20%的机会。

If you have an array of 10 items and you want 3 of them (combined equally) to have an 80% chance of being chosen then that means the other 7 have a chance of 20%.

所以...

我的工作了像这样...

I worked it out like this...

我们知道图7是20%。因此,100%是35 35 80%28。

We know 7 is 20%. So 100% is 35. 80% of 35 is 28.

在理想情况下,你应该不这样做计算的应用在纸张上:)每个项目的个体重量可能存储在一个数组或东西。这样,你可以创建动态加权阵列为任意数量的项目和不同的权重等...

Ideally you should be doing this calculation in the app not on paper :) Maybe store the individual weights of each item in an array or something. That way you can create the weighted array dynamically for any number of items and different weights etc...

您还需要其他3(共)这样的两个人,其他的10 9 28。 (让我们把它9各为便于)。

You'll need 28 of the other 3 (in total) so 9 of two of them and 10 of the other. (Let's make it 9 each for ease).

所以,你可以像这样...

So you can do like this...

NSMutableArray *weightedArray = [NSMutableArray array];

for (int i=0 ; i<originalArray.count ; ++i) {
    if (i == 3 || i == 7 || i == 9) {
        for (int count=0 ; count<9 ; ++count) {
            [weightedArray addObject:array[i]];
        }
    } else {
        [weightedArray addObject:array[i]];
    }
}

// Now you can select from the weighted array.

id randomObject = weightedArray[arc4random_uniform(weightedArray.count)];

//Index of object

NSUInteger origIndex = [originalArray indexOfObject:randomObject];

这可能是存储个体重量或东西,然后计算出你需要把在运行时数的想法。

It might be an idea to store the individual weights or something and then calculate the number you need to put at run time.

当桑迪查普曼在评论中指出,你也可以创建指数的加权阵列的原始数组,所以你将有...

As Sandy Chapman pointed out in the comments you could also created a weighted array of indices for the original array so you would have...

[0, 1, 2, 3, 3, 3, ..., 4, 5, 6, 7, 7, 7..., 8, 9, 9, 9, 9, ..., 10]

这样,你可以得到索引,而无需得到原始项目。

That way you can get the index without having to get the original item.

无论哪种方式会工作。

这篇关于iOS版 - 如何选择数组对象更多,所以后来其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆