• 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: Greg Parker <email@hidden>
  • Date: Tue, 14 Apr 2009 14:50:46 -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;


This is for a game, so it's not very critical or anything. Someone told me
that I might want to avoid a while loop at all by doing something like this:



int result = (arc4random()%9) + 1;

if (result >= activeTarget) ++result;

activeTarget = result;


That seems a little odd to me to do things that way - but I'm not really
sure.

For a game, you almost certainly don't care about the quality of your random numbers. You don't need perfect statistical behavior (like a physical simulation) or cryptographic un-guessability (like security software). All you need is "looks random enough" and "fast".


In that case, arc4random() is overkill. That's a cryptographic random number generator, so it's slow. random() would be faster and still random enough. Just remember to call srandomdev() once at the start of your program, or else you might get the same sequence of random numbers every time you run.

The increment solution is a clever one that doesn't return the same number twice in a row, but otherwise avoids unfairly returning some numbers more often than others. (Try it - write a test program that generates a few million numbers and counts the results.) It should be as good as the try-again algorithm for your purposes, and faster.


-- Greg Parker email@hidden Runtime Wrangler


_______________________________________________

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