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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![roohart](https://uploads.teamtreehouse.com/production/profile-photos/2617622/micro_MYASS.jpg)
roohart
5,529 PointsHelp needed to understand the getRandomFact method
struct FactModel {
let facts = ["Ants stretch when they wake up in the morning.",
"Ostriches can run faster than horses.",
"Olympic gold medals are actually made mostly of silver.",
"You are born with 300 bones; by the time you are an adult you will have 206.",
"It takes about 8 minutes for light from the Sun to reach Earth.",
"Some bamboo plants can grow almost a meter in just one day.",
"The state of Florida is bigger than England.",
"Some penguins can leap 2-3 meters out of the water.",
"On average, it takes 66 days to form a new habit.",
"Mammoths still walked the Earth when the Great Pyramid was being built."]
func getRandomFact() -> String {
let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(facts.count)
return facts[randomNumber]
}
}
I'm having a hard time understanding what is happening here. First of all; why is the return type of the function a string? Shouldn't it be like... an array? We are returning the facts array, right? Then; I'm not even going to go deeper in to the whole GKRandomSource -nightmare over there, but the "return facts[randomNumber] is just not clicking with me. Maybe another example of a similar situation would help?
Thaanks!
2 Answers
![Philip G](https://secure.gravatar.com/avatar/03fca2d02b25471cb7ed132a5b090aff?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Philip G
14,600 PointsHi there,
let facts = ["Ants stretch when they wake up in the morning.",
"Ostriches can run faster than horses.",
"Olympic gold medals are actually made mostly of silver.",
"You are born with 300 bones; by the time you are an adult you will have 206.",
"It takes about 8 minutes for light from the Sun to reach Earth.",
"Some bamboo plants can grow almost a meter in just one day.",
"The state of Florida is bigger than England.",
"Some penguins can leap 2-3 meters out of the water.",
"On average, it takes 66 days to form a new habit.",
"Mammoths still walked the Earth when the Great Pyramid was being built."]
Think of the facts array like a container that contains the Fact-Strings.
let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(facts.count)
The getRandomFact() Method first generates a random number which is smaller than the number of Strings in the array (because array indexes start at zero).
return facts[randomNumber]
Then, from the array, it returns the Fact-String at the index of the randomly generated number.
Hope I could help! If there's anything you don't understand, don't hesitate to ask me again :)
Best Regards,
Philip
![Caleb Kleveter](https://uploads.teamtreehouse.com/production/profile-photos/1513202/micro_caleb.jpg)
Caleb Kleveter
Treehouse Moderator 37,862 PointsSimple really once you under stand it. with this code:
let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(facts.count)
you are randomly getting a number in a range from 0 to the upper bound of the array index in facts array. In other words, I have 0 through the highest number in the array index, I close my eyes and grab one, that is my randomly selected number. I then find the string that matches the index number and put it in the randomNumber
variable box. I then return the variable box that has one string form the array. If you did return the array you would get an error because you can only assign a string, not an array, to a labels text
property.
Hope this makes sense!
![roohart](https://uploads.teamtreehouse.com/production/profile-photos/2617622/micro_MYASS.jpg)
roohart
5,529 PointsTHANK YOU! :) You cleared up the method for me!
roohart
5,529 Pointsroohart
5,529 PointsI stepped away from the computer for a while and came back to this step by step answer and I'm so grateful. Thank You! Makes sense now :)