-3

As the title says... I need to select a random object from the array (moodArray) and i think it would be easier using arc4random. If you need anymore information just let me know... Thanks in advance

rmaddy
  • 307,833
  • 40
  • 508
  • 550
user3053939
  • 121
  • 1
  • 1
  • 6

2 Answers2

8
id randomObject = [moodArray objectAtIndex:arc4random_uniform([moodArray count])];
Pang
  • 9,073
  • 146
  • 84
  • 117
Paul Dardeau
  • 2,479
  • 1
  • 11
  • 9
  • I tried this and i get the 'unused variable' warning. How do i use the variable or is this all it needs to work? – user3053939 Jan 21 '14 at 22:47
  • 1
    Oh for goodness sake! You get that warning when you declare a variable and then don't do anything with it. If you extract and object from your array into a variable called randomObject, you then need to **USE** that variable somewhere. If you're not using it, don't bother creating it. It just wastes CPU time and battery life. – Duncan C Jan 21 '14 at 23:34
5
NSString *myString = [moodArray objectAtIndex:arc4random()%[moodArray count]];
Daniel Romero
  • 1,571
  • 1
  • 20
  • 33