• 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: Tim Ramsey <email@hidden>
  • Date: Wed, 3 Aug 2005 19:54:12 -0500


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.


Ben

Devin Lane
Cocoa Programmer
Phone: (503) 775-4906
E-Mail: email@hidden
_______________________________________________

Another, less expensive, way is to use any long period psuedo-random generator that produces the random value from a seed. Save the output value to use as the next seed. Save it when you exit the application and grab it when you open. For all practical purposes, it will never repeat and no dictionary is required. Wrap it in a method to scale to a float or whatever else you might need.


Tim

--
We follow the mystics. They know where they are going. They, too, go astray, but when they go astray they do so in a way that is mystical, dark, and mysterious.
-Ryszard Kapuscinski <A Warsaw Diary (1985)>


_______________________________________________
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: James DiPalma <email@hidden>
References: 
 >Re: Random number generator (From: Ben Kazez <email@hidden>)

  • Prev by Date: Re: NSSegmentedControl Display Bug?
  • Next by Date: core data document undo problem
  • Previous by thread: Re: Random number generator
  • Next by thread: Re: Random number generator
  • Index(es):
    • Date
    • Thread