Random Word Generator?

Discussion in 'iOS Programming' started by ArtOfWarfare, Aug 26, 2008.

  1. macrumors 604


    Nov 26, 2007
    This is the code I tried using...

    in my header:
    NSMutableArray	*words;
    in my awakeFromNib:
    	[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...
    NSLog (@"%@", [words objectAtIndex: random() % 5]);
    theWord.text = (@"%@", [words objectAtIndex: random() % 5]);
    NSLog just prints (null) though and the UITextField is left blank.


    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...?
  2. macrumors member


    Sep 9, 2007
    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.

  3. macrumors regular

    Mar 13, 2008
    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:
  4. thread starter macrumors 604


    Nov 26, 2007

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

    I've tried having this...

    NSLog (@"%@", [words objectAtIndex: 1]);
    it gives me (null)... despite the fact I explicitly told it to insert @"Apple" at index 1.

    I also tried

    NSLog (@"%i", [words indexOfObject: @"Apple"]);
    which gives me 0.

    So I tried having this:
    NSLog (@"%@", [words objectAtIndex: 0]);
    which still gives me (null).

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


    What am I doing wrong?

    Edit: Okay...

    I tried adding this now:

    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...
  5. thread starter macrumors 604


    Nov 26, 2007

    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...

    	[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.
  6. Moderator emeritus


    Jul 24, 2002
    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:
    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"}
  7. macrumors 68040

    Apr 22, 2005
    It is because the array has no elements after you initialize it. Read the NSMutableArray documentation.
  8. thread starter macrumors 604


    Nov 26, 2007
    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...

    theWord.text = (@"%@", [words objectAtIndex: srandom(time(0)) % 5]);
    and I'm getting the error:
    edit: OK, and just to see what the results would be... I also tried
    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?
  9. Moderator emeritus


    Jul 24, 2002
    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.
  10. macrumors member

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


    Nov 26, 2007


    ... 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:

    - (void) generateRandomWord
    	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.

Share This Page