Re: -[NSMutableSet randomObject]
Re: -[NSMutableSet randomObject]
- Subject: Re: -[NSMutableSet randomObject]
- From: Thomas Davie <email@hidden>
- Date: Tue, 25 May 2010 19:24:21 +0100
If this is indeed the desired effect, you're much better off writing a - (NSArray *)arrayByRandomlyOrderingObjects in an NSArray category, and then calling [[mySet allObjects] arrayByRandomlyOrderingObjects];.
Bob
On 25 May 2010, at 17:54, Dave DeLong wrote:
> Ah, I see; you don't want to provide the same object twice in a row. If that's the case, is it really "random"? ;)
>
> Dave
>
> On May 25, 2010, at 10:52 AM, Dave DeLong wrote:
>
>> What about something like:
>>
>> @implementation NSSet (Random)
>>
>> - (id) randomObject {
>> NSArray * allObjects = [self allObjects];
>> if ([allObjects count] == 0) { @throw ...; }
>> return [allObjects objectAtIndex:(arc4random() % [allObjects count])];
>> }
>>
>> @end
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden