• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Random number generator
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Random number generator


  • Subject: Re: Random number generator
  • From: James DiPalma <email@hidden>
  • Date: Thu, 4 Aug 2005 02:27:52 -0700

On Aug 3, 2005, at 2:25 PM, Ben Kazez wrote:
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.

This thread's original poster seems to want integers or at least a finite set of unique numbers; his "good example" being iPod Suffle not repeating songs, implying integers.


This algorithm, which uses an NSMutableDictionary (or an NSMutableIndexSet) to remember previous random values, will lead to many useless cycles spent generating random values that have already been chosen.

A suggestion: fill an NSMutable array or an NSMutableIndexSet with a set of unique values that will be randomly selected (or randomly ordered which seems like our ultimate goal). During each cycle randomly choose one of these values and remove it from this set. By maintaining a set of unchosen unique values, each cycle will always use one call to srandomdev() or similar to generate an unchosen unique value. Your code has no need to validate this unique value since it was pre-validated by being in your unchosen set.


-jim

_______________________________________________
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


  • Follow-Ups:
    • Re: Random number generator
      • From: Mike Morton <email@hidden>
References: 
 >Re: Random number generator (From: Ben Kazez <email@hidden>)
 >Re: Random number generator (From: Tim Ramsey <email@hidden>)

  • Prev by Date: NSTableView bottleneck
  • Next by Date: How can they know?
  • Previous by thread: Re: Random number generator
  • Next by thread: Re: Random number generator
  • Index(es):
    • Date
    • Thread