• 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: Array of random, non-repeating numbers
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Array of random, non-repeating numbers


  • Subject: Re: Array of random, non-repeating numbers
  • From: Ryan Britton <email@hidden>
  • Date: Thu, 13 Jul 2006 08:23:47 -0700

For what you want, this is probably how I would do it.  Typed in Mail:

//Assume these are parameters somewhere
int low = 0;
int high = 10;

NSMutableIndexSet *source = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(low, high)];
NSMutableArray *list = [NSMutableArray arrayWithCapacity:(high - low)];


int i;
for (i = 0; i < high - low; i++)
{
int index = low - 1;
while (![source containsIndex:index])
{
index = some random index; //I'll leave it to you to pick which random generator you want.
}

[source removeIndex:index];
[list addObject:[NSNumber numberWithInt:index]]; //NSNumber is a better container for this than NSString and you can always convert with -stringValue
}




On Jul 13, 2006, at 7:46 AM, Bobby B wrote:

Hey guys,

I'm trying to write a way to generate a random array of X numbers, and
the numbers need to be between 0 and X, and not be repeating (its for
generating a random playlist.)

I've come up with the following, which works perfectly.  I'm just
curious if there is a "much better way" to do this?  (Oh, and I want
the numbers to be strings in the array)..

maxSongs = [[musicViewSongArrayController
valueForKeyPath:@"arrangedObjects.@count"] intValue];

NSMutableArray *randomArray = [NSMutableArray array];

int i = 0;
while (i < maxSongs)
{
[randomArray addObject:@"-"];
i = i + 1;
}

BOOL finished = NO;
int completed = 0;
while (finished == NO)
{
int randomIndex = (random() % maxSongs);
NSLog(@"%u", randomIndex);
if (([[randomArray objectAtIndex:completed] isEqualToString:@"-"]) &&
(!([randomArray containsObject:[NSString stringWithFormat:@"%u",
randomIndex]])))
{
[randomArray replaceObjectAtIndex: completed withObject:[NSString
stringWithFormat:@"%u", randomIndex]];
NSLog(@"%u - %u", randomIndex, completed);
completed = completed + 1;
}

if (!([randomArray containsObject:@"-"])) finished = YES;
}
NSLog(@"%@", randomArray);
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Array of random, non-repeating numbers (From: "Bobby B" <email@hidden>)

  • Prev by Date: Re: NSOutlineView: getting index of drawn row in outlineView:willDispayCell:
  • Next by Date: Re: Odd redraw issue
  • Previous by thread: Array of random, non-repeating numbers
  • Next by thread: Re: Array of random, non-repeating numbers
  • Index(es):
    • Date
    • Thread