This question comes up on this board at least once a week. A while back I wrote some code to select random words or phrases from an array, and only select any given word/phrase once. Here is the code you need:
And put these methods (and one C function) into the .m file of the same class:
Code:
//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
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]
I wrote this to use a text file delimited by carriage returns because its simple to generate a text file.
It would be trivial to change the code above to bring the array of words/phrases in from a plist instead of a text file, but that means you have to generate the plist.
All you would have to do s replace the bolded lines above with:
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
It says that error: initializer element is not constant
is not compile-time constant
expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
expected " before "
It says that error: initializer element is not constant
is not compile-time constant
expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
expected " before "
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
@Duncan C he can't be getting that as someone else would have picked up on it. He posted this in another thread, and he is trying to hijack this to solve his problem.
Hey, just posting to get this into my subscribed threads for I may want this later. It may be helpful for the future. Nice work Duncan. You can delete this or whatever, just disregard it
This question comes up on this board at least once a week. A while back I wrote some code to select random words or phrases from an array, and only select any given word/phrase once. Here is the code you need:
And put these methods (and one C function) into the .m file of the same class:
Code:
//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
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]
I wrote this to use a text file delimited by carriage returns because its simple to generate a text file.
It would be trivial to change the code above to bring the array of words/phrases in from a plist instead of a text file, but that means you have to generate the plist.
All you would have to do s replace the bolded lines above with:
Thank you Duncan for the purposeful responses to newbies like myself....
If I understand correctly, I can use this code to create a quiz of 10 Q's in the form of a +/- b =?, where a is id num 1 and b is id num 2? I still need to randomly assign each question to be + or -, would that be id num 3 populated from another plist where the only choices are +/-?
Finally, I need the quiz to appear at a specific point on screen so I need to tell text to appear at a certain CGPoint... where does that go???
How do I get the quiz to appear at certain CGPoint?
a will be id num1 and b will be id num 2
Thank you Duncan for the purposeful responses to newbies like myself....
If I understand correctly, I can use this code to create a quiz of 10 Q's in the form of a +/- b =?, where a is id num 1 and b is id num 2? I still need to randomly assign each question to be + or -, would that be id num 3 populated from another plist where the only choices are +/-?
Finally, I need the quiz to appear at a specific point on screen so I need to tell text to appear at a certain CGPoint... where does that go???
How do I get the quiz to appear at certain CGPoint?
a will be id num1 and b will be id num 2
This is a very specific question. You should start a new thread that asks for help with your specific needs.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
This is a very specific question. You should start a new thread that asks for help with your specific needs.
I want to generate a 10 Q quiz in the form of a +/- b = c where a and b are integers from (+10) to (-10) and are selected randomly for each question. Also each question should randomly choose from an add/sub operation.
I bellieve I need to use plist and arc4random but am not sure how to...
I want to generate a 10 Q quiz in the form of a +/- b = c where a and b are integers from (+10) to (-10) and are selected randomly for each question. Also each question should randomly choose from an add/sub operation.
I bellieve I need to use plist and arc4random but am not sure how to...
thank you....
ARGGGGHHHH!
To repeat:
Quote:
This is a very specific question. You should start a new thread that asks for help with your specific needs.
As I told you in a private message, you can't (and shouldn't) create new threads to this section without specific permission. You should go to the general iPhone Dev SDK page and create a new thread there with your question.
This thread is not the place for specific help developing your app, which does not have much to do with selecting random, non-repeating text.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.