• 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: 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


  • Follow-Ups:
    • Re: Random number generator
      • From: Tim Ramsey <email@hidden>
  • Prev by Date: Re: NSValueTransformer for selectedObjects controller key - bug?
  • Next by Date: Re: Inserting an (int) into an NSMutableArray
  • Previous by thread: Re: Solved Re: Fullscreen window after hiding Dock works in 10.4 but not 10.3
  • Next by thread: Re: Random number generator
  • Index(es):
    • Date
    • Thread