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

后端 未结 25 1900
有刺的猬
有刺的猬 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:15

    Without arc4Random_uniform() in some versions of Xcode(in 7.1 it runs but doesn't autocomplete for me). You can do this instead.

    To generate a random number from 0-5. First

    import GameplayKit
    

    Then

    let diceRoll = GKRandomSource.sharedRandom().nextIntWithUpperBound(6)
    

提交回复
热议问题