什么是Swift3(Xcode8 beta 1)中的随机种子等效项 [英] What is the equivalent of seeded random in Swift3 (Xcode8 beta 1)

查看:60
本文介绍了什么是Swift3(Xcode8 beta 1)中的随机种子等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在每次执行我的应用程序时启动相同的随机数列表. srand/rand不再存在.那我该怎么办?

I need to start the same random number list over every execution of my app. srand/rand do not exist anymore. What should I do then?

private extension Array {
    private func randomValues(_ seed: UInt32, num: Int) -> [Element] {
        srand (seed)

        var indices = [Int]()
        indices.reserveCapacity(num)
        let range = 0..<self.count
        for _ in 0..<num {
            var random = 0
            repeat {
                random = randomNumberInRange(range)
            } while indices.contains(random)
            indices.append(random)
        }

        return indices.map { self[$0] }
    }

推荐答案

您可以使用 Swift3中的 srand48(seed) drand48().

You can use srand48(seed) and drand48() in Swift3.

这篇关于什么是Swift3(Xcode8 beta 1)中的随机种子等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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