swift - 在 Linux 上使用 Swift 生成随机数时出错?

标签 swift linux random

我有一段代码如下:

extension Array {
  fileprivate func random() -> Element {
    let idx: Int
    #if os(Linux)
    idx = Int(random() % count)
    #else
    idx = Int(arc4random_uniform(UInt32(count)))
    #endif

    return self[idx - 1]
  }
}

当我尝试部署我的应用程序时,在使用 linux 运行 docker 时出错,问题如下:

error: binary operator '%' cannot be applied to operands of type 'Int32' and 'Int' idx = Int(rand() % count)

我将问题追溯到这个函数,但无法在此处解决,因为它已经打算为 Linux 提供替代处理...

最佳答案

错误表明 random() 返回 Int32。所以你需要转换一侧。

idx = Int(random()) % count

关于swift - 在 Linux 上使用 Swift 生成随机数时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56655883/

相关文章:

ios - 显示路线距离

ios - 无法转换 [Any] 类型的值!在强制 : in Swift3 while using for in loop 中键入 'MySpecificClass'

iOS:将核心数据提取的结果转换为数组并传递给另一个类

javascript - 单击时生成相同的随机数

haskell - Haskell 中快速随机生成器的不错选择

swift - 如果 "static"关键字用于在 swift 中定义常量/不可变,那么 "let"关键字有什么用?

linux - Docker:如何更新容器内的变量?

c - pthread mutex unlock 为什么同一个线程?

linux - 注销时后台进程 stdout 和 stderr 会发生什么?

c - 为什么用OpenMP生成随机数没有提速?