0

I'm making my first iOS app, and am in need of some help. Here is how it'll work:

User enters word in a textfield, presses a button and in the label it should be like this: [Users word] [Randomly picked word].

So I think that I should make an array with the random words and then somehow randomize them when the button is pressed to display a random word out of many after the word the user entered in the text field.

But how is it supposed to work? Here is what I was thinking:

randomizing this (don't know how though):

NSArray *words = [NSArray arrayWithObjects: @"Blue", @"Green", @"Red", nil ];

and here is the code for displaying the text from the textfield:

NSString *labeltext = [NSString stringWithFormat:@"%@", [textField text]];

if I put label.text = labeltext; then it displays the word typed by the user, but I'm stuck at the "displaying random word from array" part.

Any help appreciated!

Justin Boo
  • 10,132
  • 8
  • 50
  • 71
figdig
  • 307
  • 2
  • 13
  • possible duplicate of [Reading random values from an array](http://stackoverflow.com/questions/7047085/reading-random-values-from-an-array); see also [Picking a random object in an NSArray](http://stackoverflow.com/questions/3318902/picking-a-random-object-in-an-nsarray) – jscs May 13 '12 at 18:20
  • This should help: http://stackoverflow.com/a/56656/679254 – Brian Gesiak May 13 '12 at 18:21

2 Answers2

3
    NSArray *words = [NSArray arrayWithObjects: @"Blue", @"Green", @"Red", nil ];
    NSString *str=[words objectAtIndex:arc4random()%[words count]];
    // using arc4random(int) will give you a random number between 0 and int.
    // in your case, you can get a string at a random index from your words array 
zahreelay
  • 1,742
  • 12
  • 18
  • 1
    Drop the -1 and it's perfect. Without it you will get an indexerror. – Martol1ni May 13 '12 at 18:30
  • It gives me an error - Invalid operands to binary expression ('u_int32_t(*)(void)' and 'NSUnteger' (aka 'unsigned int')) – figdig May 13 '12 at 18:32
  • 3
    No, @Martol1ni is correct; the range of `arc4random() % [words count]` is 0 through count-1, which is exactly the range of valid indices for the array. Also, not `arc4random`, but `arc4random()` (it's a function). Also also, use `arc4random_uniform([words count])` instead of modulo. – jscs May 13 '12 at 18:34
  • @JacquesCousteau thanks for pointing out, question locked for editing – zahreelay May 13 '12 at 18:36
  • works perfectly. Now just gotta figure out how to make them non-repeatable :) special thanks to zahreelay. – figdig May 13 '12 at 18:40
  • @Jacque Cousteau, thanks for the info on arc4random_uniform. I did not know about that. First new trick of the day from Stack Overflow. Good to know there's a simple way to avoid modulo bias in calls to arc4random. Unfortunately, the Xcode help doesn't have arc4random_uniform listed at all, and I can't even find it when I search the headers. Is that function in a header that's not normally included? – Duncan C May 13 '12 at 19:40
  • @Duncan: `man arc4random_uniform` says it should be in the same header as `arc4random()`. Are you on a particularly old version of Xcode, perhaps? – jscs May 13 '12 at 19:44
0

To the OP. To make the random answers non-repeating, set up your array as an instance variable in your view controller's viewDidLoad. Also create a property remainingWords:

@property (nonatomic, retain) NSMutableArray *remainingWords;

Your viewDidLoad code would look like this:

-(void) viewDidLoad;
{
  //Create your original array of words.
  self.words = [NSArray arrayWithObjects: @"Blue", @"Green", @"Red", nil ];

  //Create a mutable copy so you can remove words after choosing them.
  self.remainingWords = [self.words mutableCopy];
}

Then you can write a method like this to get a unique word from your array:

- (NSString *) randomWord;
{
  //This code will reset the array and start over fetching another set of unique words.
  if ([remainingWords count] == 0)
    self.remainingWords = [self.words MutableCopy];

  //alternately use this code:
  if ([remainingWords count] == 0)
    return @""; //No more words; return a blank.
  NSUInteger index = arc4random_uniform([[remainingWords count])
  NSString *result = [[[remainingWords index] retain] autorelease];
  [remainingWords removeObjectAtindex: index]; //remove the word from the array.
}
Duncan C
  • 128,072
  • 22
  • 173
  • 272