开发者

iPhone - NSArray - Pick 4 different items

I've N开发者_高级运维SArray with over 100 strings.

I would like to pick 4 different strings randomly. I can write traditional way of code using for/while loops and get the task done.

But is there any better way to pick 4 different random strings?


Shuffle an array as described in JEFF LAMARCHE's blog and use first four items)


create a NSSet from your NSArray and fetch first 4 elements.


I wrote some utils as category on NSArray.

You could use it like this:

#import "NSArray+RandomUtils.h"

NSArray *array = [NSArray arrayWithObjects:@"aa", @"ab",@"c",@"ad",@"dd", nil];
NSSet *set = [array setWithRandomElementsSize:4];

This will give you a set of 4 unique random elements.

If you want to allow objects to be in your collection more than once you can do:

NSArray *array = [NSArray arrayWithObjects:@"aa", @"ab",@"c",@"ad",@"dd", nil];
NSArray *resultArray = [array arrayWithRandomElementsSize:4];

#import "NSArray+RandomUtils.h"

@implementation NSArray (RandomUtils)

-(NSMutableArray *)mutableArrayShuffled
{
    NSMutableArray *array = [[self mutableCopy] autorelease];
    [array shuffle];
    return array;
}

-(NSMutableArray *)arrayShuffled
{
    return [NSArray arrayWithArray:[self mutableArrayShuffled]];
}

-(id)randomElement
{
    if ([self count] < 1) return nil;
    NSUInteger randomIndex = arc4random() % [self count];
    return [self objectAtIndex:randomIndex];
}

-(NSSet *)setWithRandomElementsSize:(NSUInteger)size
{
    if ([self count]<1) return nil;
    if (size > [self count]) 
        [NSException raise:@"NSArrayNotEnoughElements" 
                    format:@"NSArray's size (%d) is too small to fill a random set with size %d", [self count], size];

    NSMutableSet *set = [NSMutableSet set];
    NSMutableArray *array = [self mutableArrayShuffled];

    if (size == [array count]) 
        return [NSSet setWithArray:array];

    while ([set count]< size) {
        id object = [array objectAtIndex:0];
        [array removeObjectAtIndex:0];
        [set addObject:object];

    }
    return [NSSet setWithSet:set];
}

-(NSArray *)arrayWithRandomElementsSize:(NSUInteger)size
{
    if ([self count]<1) return nil;

    NSMutableArray *array = [NSMutableArray array];
    while ([array count] < size) {
        [array addObject:[self randomElement]];
    }
    return  [NSArray arrayWithArray:array];
}
@end

@implementation NSMutableArray (RandomUtils)
-(void)shuffle
{

    NSUInteger count = [self count];
    for (NSUInteger i = 0; i < count; ++i) {
        NSUInteger nElements = count - i;
        NSUInteger n = (arc4random() % nElements) + i;
        [self exchangeObjectAtIndex:i withObjectAtIndex:n];
    }
}

@end


This has been answered multiple times in the forum. Your key to generate random numbers is

(arc4random() % 100) +1

Above code is capable of generating a random number ranging from 1 to 100. You can use this to get what you want. If you received a random number that you already got, ignore and call again to get a unique random number.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜