• 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: Best way to get a non-repeating random number?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Best way to get a non-repeating random number?


  • Subject: Re: Best way to get a non-repeating random number?
  • From: Ricky Sharp <email@hidden>
  • Date: Tue, 14 Apr 2009 15:32:45 -0500


On Apr 13, 2009, at 7:58 PM, Eric E. Dolecki wrote:

For an example I might want to generate numbers from 1 to 10 over and over.
All I want to do is when I generate a new number is not allow it to equal
the previously held value. I can imagine a few approaches but I just wanted
to make sure I was using the most accepted way of doing it.

Coming in late to this thread...

Personally, for all things random, I now use the Mersenne Twister algorithm. You can license that for free; just put in the specified acknowledgement text somewhere in your app/docs.

And, for shuffling, I use the Fisher-Yates algorithm.


For the specific problem above, you sort of have a "modified pick without replacement" scenario.


Assume you start with an array of 10 values where the values are set from 1 to 10.

The first time you pick a number, you randomly generate the index of the number (so 0..9).

That first value then becomes "picked". Your array is then modified to contain the 9 remaining values.

The 2nd, 3rd, etc. time you pick a number, you basically just pick from the nine remaining indexes. Then, put back the item you picked in the previous iteration and pull out the item you picked this iteration.

___________________________________________________________
Ricky A. Sharp         mailto:email@hidden
Instant Interactive(tm)   http://www.instantinteractive.com



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Best way to get a non-repeating random number?
      • From: Peter Castine <email@hidden>
    • Re: Best way to get a non-repeating random number?
      • From: Boyd Collier <email@hidden>
References: 
 >Best way to get a non-repeating random number? (From: "Eric E. Dolecki" <email@hidden>)
 >Re: Best way to get a non-repeating random number? (From: "Luca C." <email@hidden>)
 >Re: Best way to get a non-repeating random number? (From: Uli Kusterer <email@hidden>)
 >Re: Best way to get a non-repeating random number? (From: Michael Ash <email@hidden>)
 >Re: Best way to get a non-repeating random number? (From: Uli Kusterer <email@hidden>)
 >Re: Best way to get a non-repeating random number? (From: "Eric E. Dolecki" <email@hidden>)

  • Prev by Date: Re: Best way to get a non-repeating random number?
  • Next by Date: Re: Best way to get a non-repeating random number?
  • Previous by thread: Re: Best way to get a non-repeating random number?
  • Next by thread: Re: Best way to get a non-repeating random number?
  • Index(es):
    • Date
    • Thread