I'm trying to develop an app that gives you a random text and don't repeat. Like: I press a Button and give me a text, I press again and give another text not the same. How do you think it's the best way to do this?
Thank you,
Lucas
I'm trying to develop an app that gives you a random text and don't repeat. Like: I press a Button and give me a text, I press again and give another text not the same. How do you think it's the best way to do this?
Thank you,
Lucas
Last edited by AppStoreHQ; 07-17-2010 at 03:42 PM.
Reason: duplicate
I'm trying to develop an app that gives you a random text and don't repeat. Like: I press a Button and give me a text, I press again and give another text not the same. How do you think it's the best way to do this?
It's a little bit different. I want to develop an app that catch a random text (same categoy) and show, than show another random text (same category), and so on. What's the best way to do this?
It's a little bit different. I want to develop an app that catch a random text (same categoy) and show, than show another random text (same category), and so on. What's the best way to do this?
Thanks for ur replay.
Thanks,
Lucas
I would you a text file containing all the "random" things you want loaded. Do you want the items loaded to be truly random or just the order they're loaded in? If you want random words too, you would need a wordlist or dictionary.
I want to get random quotes, like when the user changes the page, it'll get another quote and so on
generate a random number from 1 to whatever the total number of quotes. Store the quotes into an array. use the random generated number to access that particular quote in the array. For example, you generate the number 5, it then reads the fifth element in the array.
you could also save the quotes in core data or sqldb and assign them unique ids and select them that way.
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<arraySize; index++)
[array addObject: [NSNumber numberWithInt: index]];
[self resetRandomArray];
// for (index = 0; index<=arraySize; index++)
// NSLog(@"Random word: %@", [self getRandomWord]);
}
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];
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.
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<arraySize; index++)
[array addObject: [NSNumber numberWithInt: index]];
[self resetRandomArray];
// for (index = 0; index<=arraySize; index++)
// NSLog(@"Random word: %@", [self getRandomWord]);
}
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 was seeing some posts in this forum and a lot of people said that this can be done with a .plist . The fastest way is your way or with .plist?
i think his way is kinda as complete as it gets...
pointing you towards setting up a plist doesn't solve the issue of randomizing whereas Ducan's seem to have solved all your issues within your parameters...without the whole user feedback but that's another story regardless of the storage type that you choose, Lucas.
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.
The code above is written assuming it is run from the object that owns the self.randomArray property.
Disclaimer: I banged out the code above very quickly. I'm tired, and there are probably a couple of syntax errors. You will also need to make sure that you initialize the array of word strings. The class I wrote should really be expanded to have a savePartialList method and a loadPartialList method.
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.
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<arraySize; index++)
[array addObject: [NSNumber numberWithInt: index]];
[self resetRandomArray];
// for (index = 0; index<=arraySize; index++)
// NSLog(@"Random word: %@", [self getRandomWord]);
}
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 was wondering how to get this code working..? i done the words.txt file you spoke about but how do i get the simulation iphone to get these words to appear and when i click on the screen "not button" it changes the words over and over?
The code above is written assuming it is run from the object that owns the self.randomArray property.
Disclaimer: I banged out the code above very quickly. I'm tired, and there are probably a couple of syntax errors. You will also need to make sure that you initialize the array of word strings. The class I wrote should really be expanded to have a savePartialList method and a loadPartialList method.
This code above is not working. It took me some time to get to the solution which is simple enough to make me bang my head on the wall. Not only randomArray needs to be saved. NSArray words needs to be saved also. One without other won't work.