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 09-22-2010, 04:25 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 5
Justsomedesigner is on a distinguished road
Default NSMutableArray not random enough

Hi i made a random text generator using a NSMutableArray shown in the code below ( but with 200 sentences or more ). I wondered if somebody knows how to make it more random because everytime i start the app again its the same "random" order.
What could fix my problem is save it where you left off and load that sentence where you left off when you start the app again ( but i dunno how to ).
Also i would like to know how to prevent the repeating of the sentences.
Please be free to tell me if something , entirely different would be better.
Thanks in advance...

Here's the code

Code:
#import "RandomnessViewController.h"

@implementation RandomnessViewController

- (void)viewDidLoad {
	allText = [[NSMutableArray alloc] init];
[allText addObject:@"one sentence"];
[allText addObject:@"another sentence"];
[allText addObject:@"and another"];

}

- (IBAction)setRandomText {
	NSString *text = [allText objectAtIndex:(rand() % [allText count])];
	[randomText setText:text];
	
}
Justsomedesigner is offline   Reply With Quote
Old 09-22-2010, 05:13 PM   #2 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

The 'rand' function has deterministic behavior, which means it's designed to purposely give the same sequence of random values, given the same seed.

Replace the call to 'rand' with 'arc4random'. The 'arc4random' function doesn't provide the same type of deterministic behavior as 'rand'.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 09-22-2010, 05:17 PM   #3 (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 Justsomedesigner View Post
Hi i made a random text generator using a NSMutableArray shown in the code below ( but with 200 sentences or more ). I wondered if somebody knows how to make it more random because everytime i start the app again its the same "random" order.
What could fix my problem is save it where you left off and load that sentence where you left off when you start the app again ( but i dunno how to ).
Also i would like to know how to prevent the repeating of the sentences.
Please be free to tell me if something , entirely different would be better.
Thanks in advance...

Here's the code

Code:
#import "RandomnessViewController.h"

@implementation RandomnessViewController

- (void)viewDidLoad {
	allText = [[NSMutableArray alloc] init];
[allText addObject:@"one sentence"];
[allText addObject:@"another sentence"];
[allText addObject:@"and another"];

}

- (IBAction)setRandomText {
	NSString *text = [allText objectAtIndex:(rand() % [allText count])];
	[randomText setText:text];
	
}
Use arc4random() in place of rand(). It is a self-seeding random number generator that generates better random numbers.
__________________
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-23-2010, 10:57 AM   #4 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 5
Justsomedesigner is on a distinguished road
Default

Yes thank you , both of you..
That really did the trick. Now it really IS random.
With the arc4random() instead of rand() everytime i start the app
it's in a different order.
Justsomedesigner is offline   Reply With Quote
Reply

Bookmarks

Tags
random, text

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: 347
11 members and 336 guests
2ndSegment, djohnson, Hamad, headkaze, markuschow, mistergreen2011, Objective Zero, pungs, revg, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

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