Re: -[NSMutableSet randomObject]
Re: -[NSMutableSet randomObject]
- Subject: Re: -[NSMutableSet randomObject]
- From: Ricky Sharp <email@hidden>
- Date: Tue, 25 May 2010 17:16:05 -0500
On May 25, 2010, at 11:49 AM, Michael A. Crawford wrote:
> // If the array is empty, throw an exception. The caller should know better.
> // If there is only one, return object zero every time.
> // If there are only two, alternate with every access.
> // If there are three or more, randomize but never provide the same object
> // twice in a row.
These are definitely strange rules... please note the following which describes basically all rules where n > 1
In your starting state, nothing is picked. You then pick something at random from the set. But, that item is not placed back in the set. The 2nd pick comes from the new set. The original item that was picked is now put back into the set. So part of what's going on is a "pick without replacement" as well as "pick with replacement".
Having said that, depending upon your needs, I highly recommend the Mersenne Twister algorithm for your psuedo-random generator. Depending upon speed constraints, you may also want to experiment with a Fisher-Yates shuffle of the array, then grab the 0th element as the next "random object".
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
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