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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 09-05-2010, 11:34 AM   #1 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,333
Send a message via AIM to iSDK
Default Random Text Selection

Hi, i want to make a new app, which displays different text everyday. I would have the text pasted onto a .txt file, but i just wanted to know how it could select a random text off the txt file every day.

If you could provide any code or links to how to do this, that would be a major help.

Thanks
iSDK is offline   Reply With Quote
Old 09-05-2010, 03:00 PM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

Providing the different texts are separated by new lines, you could load the text file into an NSArray (componentsSeparatedByString), then pick a random index to load from the array with arc4random() or random().
harrytheshark is offline   Reply With Quote
Old 09-06-2010, 12:34 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 138
Default

can someone post an example ?
rrichar is offline   Reply With Quote
Old 09-06-2010, 01:22 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

Take a look at NSArray's componentsSeparatedByString: in the documentation.

Then do a quick Google for "random numbers iphone sdk".
harrytheshark is offline   Reply With Quote
Old 09-06-2010, 03:31 PM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 4,814
Default

Quote:
Originally Posted by iSDK View Post
Hi, i want to make a new app, which displays different text everyday. I would have the text pasted onto a .txt file, but i just wanted to know how it could select a random text off the txt file every day.

If you could provide any code or links to how to do this, that would be a major help.

Thanks
This comes up at least a couple of times a week. Check out my post on this thread:

Random text

I wrote some working code that reads a text file, splits it out into an array with one line in each entry, and then allows you to extract lines of text at random, without repeating any.
__________________
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 09-07-2010, 06:20 AM   #6 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 3
Default More Randomness

Any one have a simple answer for a putting a random response in touches?

So I can create a button to push for random responses using arc4Random just fine.
Code:
-(IBAction) dotPressed: (id)sender
{
	int num = [answers count];
        int chosen = arc4random() % num;
        decisionText.text = [answers objectAtIndex: chosen];	
}


-(void)viewDidLoad 
{
	[super viewDidLoad];
	answers = [[NSArray alloc] initWithObjects: 
			   @"Yes", 
			   @"No", 
			   @"Maybe", nil];
}
But, for the life of me I am failing to create a random response code as a result of a touchesEnded...withEvent. There must be a simple way.
Right now I can only make one response work when object reaches its target.
I've played with several methods, but have had no success.
Here is the single response code:

Code:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
	UITouch *touch = [touches anyObject];

	CGPoint touchLocation = [touch locationInView:self.view];
	
	if (CGRectContainsPoint([flower frame], touchLocation)) 
		
	{		
		CGRect frame = viewToMove.frame;
		switch (activeView) 
		
		{
			case 0:
				response.text = @"No.";
				frame.origin.x = 20;
				frame.origin.y = 418;
				break;
I appreciate the help! I'm so close and I'm sure the answer is simple.
Which random method and where to put it...Hum.
kaczg 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: 261
20 members and 241 guests
ADY, Alsahir, beleg_1998, Dani77, e2applets, iph_s, JasonR, mer10, Monstertaco, piesia, prchn4christ, Promo Dispenser, Robiwan, Rudy, sly24, smithdale87, timle8n1, Touchmint, twerner
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,757
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

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