Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
Created January 7, 2012 20:49
Show Gist options
  • Select an option

  • Save codeswimmer/1575998 to your computer and use it in GitHub Desktop.

Select an option

Save codeswimmer/1575998 to your computer and use it in GitHub Desktop.
iOS: Generating random, non-repeating text
// iOS: Generating random, non-repeating text
/*
Selects random words or phrases from an array, and only select any given word/phrase once.
To use it:
-Create a file called words.txt containing a list of words or phrases separated by carriage returns.
-Drag that file into the resources portion of your project, so it will be included in the bundle.
-Call [self buildRandomWordArray] to do the initial setup. That reads the file and populates an array of random indexes.
-Call [self getRandomWord] to get a new random word or phrase. That word/phrase will be deleted from the list and not used again. Once the list is exhausted, the getRandomWord will return nil;
-To start over and get the same list of words in a different order, call [self resetRandomArray];
_
*/
NSArray* words;
NSMutableArray* randomArray;
NSMutableArray* array;
}
@property (nonatomic, retain) NSArray* words;
@property (nonatomic, retain) NSMutableArray* array;
@property (nonatomic, retain) NSMutableArray* randomArray;
//This is the "sort" function that puts the items in random order.
//It uses a random number generator to decide the ordering of items in the array
NSInteger randomize(id num1, id num2, void *context)
{
int rand = arc4random() %2;
if (rand)
return NSOrderedAscending;
else
return NSOrderedDescending;
}
//This function populates "randomArray" with an array of indexes in random order.
//Once the getRandomWord method returns nil, call resetRandomArray to
//repopulate the randomArray with the same list of words in a different random order.
- (void) resetRandomArray;
{
//Copy over our starting "array" containing all our indexes in order.
[randomArray setArray: array ];
//Use a trick to "sort" the array into random order.
//this trick involves using a sort function "randomize" that puts
//the items in random order.
[randomArray sortUsingFunction: randomize context:NULL];
}
-(NSString*) getRandomWord;
{
if ([randomArray count] ==0)
return nil;
NSString* result;
NSInteger randomIndex = [[randomArray lastObject] intValue];;
[randomArray removeLastObject];
result = [words objectAtIndex: randomIndex];
return result;
}
-(void) buildRandomWordArray;
{
NSInteger index;
NSError* theError;
NSString *path = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"txt"];
NSString* text = [NSString stringWithContentsOfFile: path
encoding: NSUTF8StringEncoding
error: &theError];
self.words = [text componentsSeparatedByString: @"\n"];
int arraySize = [words count];
self.array = [NSMutableArray arrayWithCapacity:arraySize];
self.randomArray = [NSMutableArray arrayWithCapacity:arraySize];
//This code fills "array" with index values from 0 to the number of elements in the "words" array.
for (index = 0; index<arraySize; index++)
[array addObject: [NSNumber numberWithInt: index]];
[self resetRandomArray];
// for (index = 0; index<=arraySize; index++)
// NSLog(@"Random word: %@", [self getRandomWord]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment