• 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: Peter Duniho <email@hidden>
  • Date: Tue, 14 Apr 2009 14:16:36 -0700

On Apr 14, 2009, at 1:32 PM, Eric E. Dolecki wrote:

My apologies - what  I meant to type was that I AM doing this:

int tmp = (arc4random())+1;

while (tmp == activeTarget) {

 tmp = (arc4random())+1;

}

activeTarget = tmp;

So, you are generating a random number between 1 and 10, inclusive.

Given the relatively small number of values, a shuffle is a very appropriate way to generate a non-repeating but otherwise-random sequence of numbers.

Note that if your only requirement is simply that you don't get the same number twice in a row, rather than that you want to see all the numbers in your set of possible values before any number repeats, a shuffle is overkill. But also note that if _that_ is your requirement (i.e. "that you don't get the same number twice in a row"), you're not really dealing with a random sequence, and may in fact have a design error. :)

Pete
_______________________________________________

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


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: "Luca C." <email@hidden>)
 >Re: Best way to get a non-repeating random number? (From: Jean-Daniel Dupas <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: "Eric E. Dolecki" <email@hidden>)
 >Re: Best way to get a non-repeating random number? (From: Andrew Farmer <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: Reading in dictionary from txt file: options for speed
  • 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