Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Simple iPhone App (iOS7) Creating a Data Collection Randomization

Golan David
PLUS
Golan David
Courses Plus Student 892 Points

Number 5

It's can be number 5 randomly chosen from the Hat in the movie it will be number 6 or 5? while math says we are start counting from 0 => 0,1,2,3,(4)< it's correct?

Stone Preston
Stone Preston
42,016 Points

can you explain a little more clearly what you are asking?

5 Answers

Golan David
PLUS
Golan David
Courses Plus Student 892 Points

Please watch the tutorial movie [when the number 5 is related to the Hat] my opinion it's sound a mistake or I didn't understand it correctly the calculation.

Stone Preston
Stone Preston
42,016 Points

when you call random(10), it returns a random number from 0 -9. (the hat is filled with numbers from 0 - 9) so in the video, the function is called and it returns 5. (we drew a 5 from the hat). the number 5 is used to index the array of predictions.

Golan David
PLUS
Golan David
Courses Plus Student 892 Points

Okay but when I say objectAtIndex:5 what number from the scope than need to be choose? as I understand from your words the "system" choose Random number from 0-9?

Stone Preston
Stone Preston
42,016 Points

objectAtIndex 5 would choose the object at index 5 in the array. this is what happens in the code:

//choose a random number from 0 to the length of the array. this is like drawing from the hat filled with numbered balls
int random = arc4random_uniform(self.predictions.count);

//set the text of the label using the random number as the index of the array. so if the random number was 5, it would choose the 6th item in the array. 
self.predictionLabel.text = [self.predictions objectAtIndex:random]
Golan David
PLUS
Golan David
Courses Plus Student 892 Points

Thanks for your help! I'll do trial and error to the subject

Thanks!) Sincerely, Golan