Swift arc4random_uniform(max) in Linux

后端 未结 4 1031
猫巷女王i
猫巷女王i 2021-02-14 00:06

I\'m working with Swift in Ubuntu, and I am getting an error that arc4random is an unresolved identifier. More information on this known bug here. Basically, the function only e

4条回答
  •  一个人的身影
    2021-02-14 00:15

    I went with something like this for 4-digit random numbers:

    #if os(Linux)
     srandom(UInt32(time(nil)))
     randomString = String(format: "%04d", UInt32(random() % 10000))
    #else
     randomString = String(format: "%04d", Int(arc4random_uniform(10000)))
    #endif
    

    Edit: Note that the call to srandom(UInt32(time(nil))) should be outside a function/loop, otherwise it will produce the same value over and over again

提交回复
热议问题