Re: Best way to get a non-repeating random number?
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