Re: Random sorting
Re: Random sorting
- Subject: Re: Random sorting
- From: Andrew Pinski <email@hidden>
- Date: Sun, 5 Jan 2003 20:58:01 -0800
Two things:
1. do not seed the random number every time
2. rand does not produce random number in the lower bits use
random(3)* instead.
* random has a man page in the third section aka `man 3 random'.
Thanks,
Andrew Pinski
On Sunday, Jan 5, 2003, at 20:42 US/Pacific, 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.
_______________________________________________
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.