• 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: Random sorting
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Random sorting


  • Subject: Re: Random sorting
  • From: "John C. Randolph" <email@hidden>
  • Date: Mon, 6 Jan 2003 09:46:03 -0800

Some years ago I worked on a card game and what we did was not to shuffle the array but rather pick random values out of the array. The card-dealing algorithm was:

pick a random number in the range of 0 to the number of cards.
Choose the card in that slot.
take the last card, and put it in the slot you just picked.

The first time, you pick a number from 0 to 51. The next time, you pick a number from 0 to 50, and so on.

-jcr

On Sunday, January 5, 2003, at 08:42 PM, Andrew Merenbach wrote:

I would like to add a random-sorting algorithm for arrays to my
program, and figured that the easiest way would be to add a category to
NSString that would return a random result. Here's my "algorithm":

- (NSComparisonResult)randomCompare:(NSString *)otherString
{
int n;

srand(rand()%time(NULL));
n = rand()%3;

switch(n) {
case 0: return NSOrderedAscending; break;
case 1: return NSOrderedSame; break;
case 2: return NSOrderedDescending; break;
}

return NSOrderedSame;
}

Is this a "bad" (or inefficient) way of randomly sorting an array of
strings?

Take care,
Andrew
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Random sorting (From: Andrew Merenbach <email@hidden>)

  • Prev by Date: Re: Working with 32-bit Unicode (NSString stringWithUTF32String: (const UTF32Char *) bytes needed)
  • Next by Date: Re: Working with 32-bit Unicode (NSString stringWithUTF32String: (const UTF32Char *) bytes needed)
  • Previous by thread: Re: Random sorting
  • Next by thread: Re: Random sorting
  • Index(es):
    • Date
    • Thread