用Swift生成随机数

我需要生成一个随机数。

它看起来arc4random函数不再存在以及arc4random_uniform函数

我具有的选项是arc4random_stir(),arc4random_buf(UnsafeMutablePointer< Void>,Int)和arc4random_addrandom(UnsafeMutablePointer< UInt8>,Int32)。

我找不到任何文档的功能,没有在头文件中的注释提示

arc4random()

Returns a random number in the range of 0 to 2^32.

Good to kNow: If you do not a number of that super large range then
you can also use the functions rand() and random() which return a
random number from “just” half the range.

drand48()

Returns a random double between 0.0 and 1.0

Be aware that the return value is Double and not Int! To learn more
about drand48() and its “family” of similar functions check the 07000

arc4random_uniform(UInt32)

Returns a random number between 0 and the inserted parameter minus 1.

For example arc4random_uniform(3) may return 0,1 or 2 but not 3.

That function is the most common random function but it has one minor
issue: dealing with UInt32 in Swift is not very common so a conversion
to Int is normally done like in the following example:

let random = Int(arc4random_uniform(3))

那么为什么要arc4random()和arc4random_uniform呢?除了缩小范围之外,均匀性还考虑了在将十进制数转换为二进制时产生的模偏差。模运算符使一些数字更频繁地出现。

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...