0

I'm trying to use a random number extension I created that worked in swift 2 and fails in Swift 3. Not sure why.

Here is the code.

let aRandomInt = Int.random(range:0...2)

//Extention to INT to create random number in range.
extension Int
{
    static func random(range: Range<Int> ) -> Int
    {
        var offset = 0

        if range.lowerBound < 0   // allow negative ranges
        {
            offset = abs(range.lowerBound)
        }

        let mini = UInt32(range.lowerBound + offset)
        let maxi = UInt32(range.upperBound   + offset)

        return Int(mini + arc4random_uniform(maxi - mini)) - offset
    }
}

The error I get is...

No '...' candidates produce the expected contextual result type 'Range'

This error occurs on the line that says let aRandomInt = Int.random(range:0...2)

Joseph Astrahan
  • 8,659
  • 12
  • 83
  • 154
  • Does this help http://stackoverflow.com/questions/37915972/overloads-for-exist-with-these-result-types-closedrangebound-countable ? – Martin R Sep 28 '16 at 09:07
  • quite possibly looks like they changed ranges a lot, let me read that article. – Joseph Astrahan Sep 28 '16 at 09:09
  • As in the referenced thread, you just have to replace `Range` by `ClosedRange` in the function definition. – Martin R Sep 28 '16 at 09:11
  • Looks like it does, yeah if you change to this.. let lo = 0; let hi = 2; let aRandomInt = Int.random(range:lo...hi), then like you said change it to ClosedRange you can put that as answer it worked! – Joseph Astrahan Sep 28 '16 at 09:12

0 Answers0