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:rsh...@instantinteractive.com
Instant Interactive(tm)   http://www.instantinteractive.com



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to