0

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

Will
  • 11
  • 2

1 Answers1

1

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
}
dilaver
  • 674
  • 3
  • 17
  • 1
    It is way overkill to use GameKit.framework just to generate a random number. – Ozgur Vatansever May 11 '17 at 08:37
  • What would go into the third int slot and does the second line need me to add in custom variables at the max and min slots to it to make it use the variables set through my code? – Will May 11 '17 at 08:41
  • This function takes only two parameters. Third `Int` is the return type. You just need to call this function like this: `let randomNumber = randomBetween(min: 4, max: 10)` – dilaver May 11 '17 at 08:44
  • Sorry 1 more thing, i could change what you used as 4 and 10 to custom variables as long as they were integers right? – Will May 11 '17 at 08:46
  • Yes, you can change them to any integers – dilaver May 11 '17 at 08:46