Swift randomizer with variables

Solution for Swift randomizer with variables
is Given Below:

I have two String Type values as Int number.
I like to randomize between these two.

With this code:

let random = Int.random(in: myvar1...myvar2)

it does not work. How can i fix it?

I assume your variables look like this?

let myvar1: String = "1"
let myvar2: String = "10"

Those are both Strings, so myvar1...myvar2 becomes a range of String.

However, the random(in:) method takes in a range of Int, so you’ll first need to convert them to Ints.

if let myvar1Int = Int(myvar1), let myvar2Int = Int(myvar2) {

    let random = Int.random(in: myvar1Int...myvar2Int)

    print(random) /// Result: 6

}