-2

Working in Xcode. I can't quite figure this one out. What I'd like to do is have an array of strings "one", "two", "three" etc. I want to be able to have a label which displays one of these strings, chosen at random. Any ideas?

What I've tried so far is this: I've created an array as such:

NSString *yes0 = @"yes";
NSString *yes1 = @"YES";
NSString *yes2 = [NSString stringWithFormat:@"%@", yes1];
NSArray *googleArray = [NSArray arrayWithObjects:yes0, yes1, yes2, nil];

What I'm trying to figure out is how to display an element of this array randomly. I've figured out how to pass a variable to the label which would be as such:

strValue = @"";
yourLabel.text = strValue;

I'm guessing after I have randomly chosen an element from the array, I set strValue equal to that and hence it gets set as my label. I do not understand the random choosing of the string itself.

Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
  • 1
    Can you please tell us what you've tried? – rocky Aug 02 '13 at 19:10
  • 2
    Obviously you didn't try anything, even searching in google, this is the first anserw you will find: http://stackoverflow.com/questions/7580354/get-random-object-from-array – Tarek Hallak Aug 02 '13 at 19:13

1 Answers1

1

Try this:

 int randomNumber = arc4random() % [yourArray count];
 NSString *string = [yourArray objectAtIndex:randomNumber];
Abdullah Shafique
  • 6,878
  • 8
  • 35
  • 70