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
Mitch Ford
4,610 PointsRandom number in Swift 3
I am having trouble trying to generate a random number in Swift 3 for use in my game. I have looked around online and can't get arc4random to work for me. Can anyone offer any advice?
1 Answer
kasaltrix
9,491 PointsHi Mitch,
You can use GameKit framework to generate a random number in Swift as shown in one of the iOS courses here on Treehouse. The upperBound here represents the maximum value up to which you want to generate the random number. Hope this helps :)
import GameKit
let randomNumber = GKRandomSource.sharedRandom().nextInt(upperBound: 5)
Mitch Ford
4,610 PointsMitch Ford
4,610 PointsThank you! i had also tried using GameKit framework but looks like the syntax i was using was out of date! This solved my problem, thanks!