Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-16-2010, 12:01 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 39
Lucasdms is on a distinguished road
Question Random Text

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
Lucasdms is offline   Reply With Quote
Old 07-16-2010, 12:10 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 41
Yellow is on a distinguished road
Default

Quote:
Originally Posted by Lucasdms View Post
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
Hi Lucas,

I'm not too sure the scope that you are looking at, hopefully this helps
Random word generator - Stack Overflow

cheers
yellow
Yellow is offline   Reply With Quote
Old 07-16-2010, 01:26 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 39
Lucasdms is on a distinguished road
Question

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
Lucasdms is offline   Reply With Quote
Old 07-16-2010, 04:29 PM   #4 (permalink)
iOS Developer
 
chaseacton's Avatar
 
Join Date: Feb 2009
Location: United States
Posts: 541
chaseacton is on a distinguished road
Send a message via AIM to chaseacton Send a message via Skype™ to chaseacton
Default

Quote:
Originally Posted by Lucasdms View Post
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.
__________________
Freelance Inquiries:
www.chaseacton.com/services

Apps:
chaseacton is offline   Reply With Quote
Old 07-18-2010, 05:46 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 39
Lucasdms is on a distinguished road
Default

I want to get random quotes, like when the user changes the page, it'll get another quote and so on
Lucasdms is offline   Reply With Quote
Old 07-18-2010, 07:42 PM   #6 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Boston, MA
Posts: 135
carbonbasednerd is on a distinguished road
Default

Quote:
Originally Posted by Lucasdms View Post
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.
carbonbasednerd is offline   Reply With Quote
Old 07-18-2010, 08:38 PM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Lucasdms View Post
I want to get random quotes, like when the user changes the page, it'll get another quote and so on
Ok,

For some reason I took it upon myself to write some code to do this. Here's what you need:

Put these ivars and properties in the header of the class that needs to generate random words:


Code:
	NSArray* words;
	NSMutableArray* randomArray;
	NSMutableArray* array;
}
@property (nonatomic, retain)			NSArray* words;
@property (nonatomic, retain)			NSMutableArray* array;
@property (nonatomic, retain)			NSMutableArray* randomArray;
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];
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-18-2010, 09:44 PM   #8 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 41
Yellow is on a distinguished road
Default

hi lucas,

just realised that i kept reading your threads and it got me wondering...are the users going to be able to write their own quotes too?

cheers,
yellow
Yellow is offline   Reply With Quote
Old 07-19-2010, 06:22 AM   #9 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 39
Lucasdms is on a distinguished road
Default

No. The users will just see, and I don't know may when I update I can put an option that the user can send me the quote and I can approve or not.
Lucasdms is offline   Reply With Quote
Old 07-19-2010, 06:35 AM   #10 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 39
Lucasdms is on a distinguished road
Question

Quote:
Originally Posted by Duncan C View Post
Ok,

For some reason I took it upon myself to write some code to do this. Here's what you need:

Put these ivars and properties in the header of the class that needs to generate random words:


Code:
	NSArray* words;
	NSMutableArray* randomArray;
	NSMutableArray* array;
}
@property (nonatomic, retain)			NSArray* words;
@property (nonatomic, retain)			NSMutableArray* array;
@property (nonatomic, retain)			NSMutableArray* randomArray;
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?

Thanks,
Lucas
Lucasdms is offline   Reply With Quote
Old 07-19-2010, 07:23 AM   #11 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 41
Yellow is on a distinguished road
Default

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.
Yellow is offline   Reply With Quote
Old 07-24-2010, 08:13 AM   #12 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Lucasdms View Post
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?

Thanks,
Lucas
I doubt if you could detect the difference in speed. Either approach should be plenty fast enough.

A text file seems easier to create, so I used that.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-12-2010, 09:02 PM   #13 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 2
defou is on a distinguished road
Default

Hi everyone,

I am new to the iPhone SDK and programing in general. I have managed to implement this code into my app that is not that different than this example.

What is the best way to save the remaining, unused words? I want them to be reloaded on next app startup.

For example my words.txt file has 400 words. In one game user retrieves only 50 words so the next time he would play he gets those 350 unused words.

I would appreciate any help.
defou is offline   Reply With Quote
Old 10-12-2010, 09:32 PM   #14 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by defou View Post
Hi everyone,

I am new to the iPhone SDK and programing in general. I have managed to implement this code into my app that is not that different than this example.

What is the best way to save the remaining, unused words? I want them to be reloaded on next app startup.

For example my words.txt file has 400 words. In one game user retrieves only 50 words so the next time he would play he gets those 350 unused words.

I would appreciate any help.

The code I posted creates an array "randomArray" which contains a randomized array of word indexes.

As you select random words, it deletes entries from the randomArray.

If you want to save a partially used array of random words, save randomArray with code like this:

Code:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: randomArray forKey: @"randomArray"];
[defaults synchronize];
Then, when you want to read the array back, do this:


Code:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSArray* tempArray = [defaults objectForKey: @"randomArray"];
self.randomArray = [[NSMutableArray arrayWithCapacity: [tempArray count]] addObjectsFromArray: tempArray];
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-25-2010, 07:06 PM   #15 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 1
marcokermit is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
Ok,

For some reason I took it upon myself to write some code to do this. Here's what you need:

Put these ivars and properties in the header of the class that needs to generate random words:


Code:
	NSArray* words;
	NSMutableArray* randomArray;
	NSMutableArray* array;
}
@property (nonatomic, retain)			NSArray* words;
@property (nonatomic, retain)			NSMutableArray* array;
@property (nonatomic, retain)			NSMutableArray* randomArray;
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?

thank you for your help in advance
marcokermit is offline   Reply With Quote
Old 11-06-2010, 06:55 AM   #16 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 2
defou is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
The code I posted creates an array "randomArray" which contains a randomized array of word indexes.

As you select random words, it deletes entries from the randomArray.

If you want to save a partially used array of random words, save randomArray with code like this:

Code:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: randomArray forKey: @"randomArray"];
[defaults synchronize];
Then, when you want to read the array back, do this:


Code:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSArray* tempArray = [defaults objectForKey: @"randomArray"];
self.randomArray = [[NSMutableArray arrayWithCapacity: [tempArray count]] addObjectsFromArray: tempArray];
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.

Save...
Code:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: randomArray forKey: @"randomArray"];
[defaults synchronize];

NSUserDefaults*saveWords  = [NSUserDefaults standardUserDefaults];
[saveWords setObject: words forKey: @"words"];
[saveWords synchronize];
Load...

Code:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSArray* tempArray = [defaults objectForKey: @"randomArray"];
self.randomArray = [NSMutableArray arrayWithArray: tempArray];
	
NSUserDefaults* saveWords = [NSUserDefaults standardUserDefaults];
NSArray* theArray = [saveWords objectForKey: @"words"];
self.words = [NSArray arrayWithArray: theArray];
I don't know much about Objective C but reading the code and learning the logic of it helps a lot.

Tnx
defou is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 335
15 members and 320 guests
akphyo, alexP, appservice, cgokey, EXOPTENDAELAX, flamingliquid, guusleijsten, mariano_donati, ohmniac, Paul Slocum, PavelSea, Rudy, SLIC, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,653
Threads: 94,115
Posts: 402,888
Top Poster: BrianSlick (7,990)
Welcome to our newest member, ohmniac
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:39 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0