Re: anyObject (random object from a set)
Re: anyObject (random object from a set)
- Subject: Re: anyObject (random object from a set)
- From: Bill Bumgarner <email@hidden>
- Date: Thu, 31 Jan 2002 12:00:07 -0500
I was confused by that the first time I encountered it, as well. I was
hacking up a quickie to solve some random problem and needed to grab a
random object from an array-- a truly random object. I went down the
same path, then realized that 'returns a random object' means 'returns the
most conveniently accessed object in the set' instead of 'uses a
randomizing facility to return a truly random object in the set'.
In other words, an NSSet is implemented such that it can do set related
operations-- member test, union, intersection, etc...-- on a bunch of
objects very efficiently. The internal implementation may vary depending
on the size of the set; it may start out as just a wrapper on an NSArray
and move to some kind of hashing system as the size grows. Only the
source knows...
In any case, you will need to write a method to randomly retrieve an
object if you really need to retrieve a random object.
If you want to do a truly tricked out implementation, write a
RandomEnumerator class....
b.bum
On Thursday, January 31, 2002, at 11:37 AM, email@hidden.
com wrote:
I am trying to use the NSSet method anyObject which, according to the
docs, returns a random object selected from the set or, if the set is
empty, nil.
b.bum
I ride tandem with the random....
.... things don't happen the way I planned them.