Linux中的Swift Arc4random_uniform(max) [英] Swift arc4random_uniform(max) in Linux

查看:77
本文介绍了Linux中的Swift Arc4random_uniform(max)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Ubuntu中使用Swift,但遇到一个错误,即arc4random是一个未解决的标识符.有关此已知错误的更多信息,请此处.基本上,该功能仅存在于BSD发行版中.我已经尝试了模块映射头文件,apt-getting包,并且得到了越来越多的错误,这是不值得追求的,因为这一功能并不经常使用.

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 exists in BSD distros. I've tried module mapping header files, apt-getting packages, and I get more and more errors, which is not worth pursuing since this one function is not used very often.

是否有任何函数可以获取具有与Linux中的Swift兼容的上限参数的伪随机数?

Are there any functions to get pseudo random numbers with an upper-bound parameter that is compatible with Swift in Linux?

推荐答案

我对4位随机数使用了类似的方法:

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

请注意,对srandom(UInt32(time(nil)))的调用应在函数/循环之外,否则将一遍又一遍地产生相同的值

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

这篇关于Linux中的Swift Arc4random_uniform(max)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆