Re: Random number generator
Re: Random number generator
- Subject: Re: Random number generator
- From: Ben Kazez <email@hidden>
- Date: Wed, 03 Aug 2005 15:25:57 -0400
Devin Lane wrote:
On Aug 2, 2005, at 7:52 PM, Ben Kazez wrote:
On Tue, 2 Aug 2005 13:32:58 +0200, Ludwig Villiger
<email@hidden> wrote:
Hi guys,
I am looking for a random number generator in Cocoa. For now I use
srandomdev(). But, I am looking for a random generator, which creates
a number and can remember it. So the number will appear no more.
A good example was the auto fill for the iPod Shuffle: A song is
listed only one time.
I don't believe such a class exists, but it's pretty easy to make
your own class to do this. One way is to have the class store an
NSMutableDictionary whose keys are NSNumbers indicating numbers that
have already been used; the values can be anything you want. Then the
randomizing method just checks that there are fewer keys in the
dictionary than possible random numbers, and if so, keeps picking
random numbers until it finds one that doesn't exist as a key in the
NSMutableDictionary. If there aren't fewer keys than random numbers,
then there are no remaining possible values.
Note that this implementation assumes random int values; this would
be require some (maybe a lot) of tweaking to work with floats or
doubles.
NSMutableIndexSet is much faster, since it can store numbers directly.
It also removes the hassle of going from int > NSNumber and vise-versa.
Oops, you're right. The only problem with NSMutableIndexSet is if the
original poster wants to get unique random floats.
Ben
Devin Lane
Cocoa Programmer
Phone: (503) 775-4906
E-Mail: email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden