How does one generate a random number in Apple's Swift language?

后端 未结 25 1899
有刺的猬
有刺的猬 2020-11-22 09:51

I realize the Swift book provided an implementation of a random number generator. Is the best practice to copy and paste this implementation in one\'s own program? Or is t

25条回答
  •  轮回少年
    2020-11-22 10:20

    Swift 4.2

    Swift 4.2 has included a native and fairly full-featured random number API in the standard library. (Swift Evolution proposal SE-0202)

    let intBetween0to9 = Int.random(in: 0...9) 
    let doubleBetween0to1 = Double.random(in: 0...1)
    

    All number types have the static random(in:) which takes the range and returns the random number in the given range

提交回复
热议问题