Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,616
6,145
This is the code I tried using...

in my header:
Code:
NSMutableArray	*words;

in my awakeFromNib:
Code:
	[words insertObject: @"Apple" atIndex: 1];
	[words insertObject: @"Ball" atIndex: 2];
	[words insertObject: @"Car" atIndex: 3];
	[words insertObject: @"Dog" atIndex: 4];
	[words insertObject: @"Fork" atIndex: 5];

and then to randomly display one of the words...
Code:
NSLog (@"%@", [words objectAtIndex: random() % 5]);
theWord.text = (@"%@", [words objectAtIndex: random() % 5]);

NSLog just prints (null) though and the UITextField is left blank.

Also...

I anticipate writing out every single word that it could display will be very time consuming. The iPhone already has a built in dictionary for spell checking... is there some way I can randomly choose a word from that?

Another thing... I don't think random will be very... random. It seems like each time someone launches the application it would run through the same list of words. Is there some way I can use the current time as a seed for the random number...?
 

davedelong

macrumors member
Sep 9, 2007
50
0
Right here.
This probably won't solve your problem, but Arrays are 0-based, not 1-based. So if you get a random number and % 5, you will NEVER get 5 as a result. You'd get 0. And you don't have anything in the 0 slot of your array.

You can seed the random number generator using the srandom() function, IIRC. Something like srandom(time()) I think should work.

Dave
 

drivefast

macrumors regular
Mar 13, 2008
128
0
see also SecRandomCopyBytes() but i am almost sure it uses the same algorithm for the generator. by the way, this function never worked when you build for the simulator. you have to build for device :confused:
 

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,616
6,145
Ok...

there's something completely messed up with my array...

I've tried having this...

Code:
NSLog (@"%@", [words objectAtIndex: 1]);

it gives me (null)... despite the fact I explicitly told it to insert @"Apple" at index 1.

I also tried

Code:
NSLog (@"%i", [words indexOfObject: @"Apple"]);

which gives me 0.

So I tried having this:
Code:
NSLog (@"%@", [words objectAtIndex: 0]);

which still gives me (null).

The last thing I tried before posting this was:
Code:
NSLog (@"%@", words);
Which gives me (null).

:confused:

What am I doing wrong?

Edit: Okay...

I tried adding this now:

Code:
words = [[NSMutableArray alloc] init];

It seems to work now...

:/

Someone want to explain allocating/deallocating and initializing to me? I think I'm going to end up with a lot of memory leaks since I don't understand it at all...

Thus far in the 700 lines of code for my program, this is the first time I had to use alloc or init...
 

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,616
6,145
Success!

Things learned...

1.) Arrays must be allocated and initialized.

2.) When you're inserting objects into particular indexes of an array, the previous index must be filled already.

For example...

Code:
	[words insertObject: @"Apple" atIndex: 0];
	[words insertObject: @"Ball" atIndex: 1];
	[words insertObject: @"Car" atIndex: 2];
	[words insertObject: @"Fork" atIndex: 4];
	[words insertObject: @"Dog" atIndex: 3];

doesn't work.. it'll crash. The 3rd index must be filled before the 4th one can be given anything. I don't know why... but now i know. You have to insert something in the 0th index first... you can't start with the 1st.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Instead of using insertObject:atIndex: it'd be much easier to use addObject: that just puts the object at the end of the array. Probably more efficient too.

For example:
Code:
NSMutableArray *words = [[NSMutableArray alloc] init];
[words addObject: @"Apple"];
[words addObject: @"Ball"];
[words addObject: @"Car"];
[words addObject: @"Fork"];
[words addObject: @"Dog"];

Will result in the array {@"Apple",@"Ball",@"Car",@"Fork",@"Dog"}
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
Success!

Things learned...

1.) Arrays must be allocated and initialized.

2.) When you're inserting objects into particular indexes of an array, the previous index must be filled already.

For example...

Code:
	[words insertObject: @"Apple" atIndex: 0];
	[words insertObject: @"Ball" atIndex: 1];
	[words insertObject: @"Car" atIndex: 2];
	[words insertObject: @"Fork" atIndex: 4];
	[words insertObject: @"Dog" atIndex: 3];

doesn't work.. it'll crash. The 3rd index must be filled before the 4th one can be given anything. I don't know why... but now i know. You have to insert something in the 0th index first... you can't start with the 1st.

It is because the array has no elements after you initialize it. Read the NSMutableArray documentation.
 

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,616
6,145
OK... so I found an example of using time as the seed for srandom... the idea being that each time the program is run it'll generate a different order of words.

My code is...

Code:
theWord.text = (@"%@", [words objectAtIndex: srandom(time(0)) % 5]);

and I'm getting the error:
error: void value not ignored as it ought to be.

edit: OK, and just to see what the results would be... I also tried
Code:
theWord.text = (@"%@", [words objectAtIndex: time(0) % 5]);
It gave me no errors (which suggests srandom is what is returning a void that should be ignored,) but I found it was too predictable.

2x edit: C documentation is... different. Less organized... less detailed.

Anyways... random returns a long while srandom returns a void. :confused: what's the purpose of generating a random number if it doesn't return what the random number it generated was?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
srandom does exactly what it says it does: it seeds the random number generator. It doesn't get a random number: that's what random does.

The normal usage is to call srandom once at the start of the program to seed the generator then random one or more times to get random numbers.
 

grimjim

macrumors member
May 24, 2003
75
0
I always thought that srandom() was merely the seed function for random().
So you would use:
Code:
srandom(time(NULL));
on its own to seed the random number generator as the app launches, and then
Code:
 theWord.text = ([NSString stringWithFormat:@"%@", [words objectAtIndex: random() % 5]]);
 

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,616
6,145
:D

Thanks!

... if I'm not mistaken... my program is now ready to be released!

I think I'll keep testing it though for a while just to verify it does all work...

Oh, for those interested, the code I finally used is:

Code:
- (void) generateRandomWord
{
	srandom(time(0));
	theWord.text = (@"%@", [words objectAtIndex: random() % [words count]]);
}

So that I don't have to change the code each time I add new words, I made it so it finds [words count] instead of using a fixed number.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.