Search code examples
swiftxcoderandomarc4random

Swift 3 - Make a Random number generate in between 2 integer variables


i'm semi new to xcode and swift, and i'm just making some small apps to play around with some stuff. I wanted to try and make a number generator in which you set the minimum number and maximum number and the app will pick a random number in between the two. Would this be possible?

I all that I need help with is making a random number appear in between two variable integers just to clarify. Thanks


Solution

  • Swift 4.2 Edit:

    func randomBetween(min: Int, max: Int) -> Int {
        return Int.random(in: min ..< max) 
    }
    

    import GameKit
    
    func randomBetween(min: Int, max: Int) -> Int {
        return GKRandomSource.sharedRandom().nextInt(upperBound: max - min) + min
    }