从Swift中的数组中获取随机元素

Pat*_*ric 13 arrays random shuffle swift

我有一个像这样的数组:

var names: String = [ "Peter", "Steve", "Max", "Sandra", "Roman", "Julia" ]
Run Code Online (Sandbox Code Playgroud)

我想从该数组中获取3个随机元素.我是从C#来的,但是我很快就不知道从哪里开始.我想我应该首先将阵列洗牌,然后从中挑选前3个项目?

我尝试使用以下扩展名对其进行随机播放:

extension Array
{
    mutating func shuffle()
    {
        for _ in 0..<10
        {
            sort { (_,_) in arc4random() < arc4random() }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但它然后说"'()'在"shuffle()"的位置不能转换为'[Int]'".

为了挑选我使用的一些元素:

var randomPicks = names[0..<4];
Run Code Online (Sandbox Code Playgroud)

到目前为止看起来不错.

怎么洗牌?或者有没有人有更好/更优雅的解决方案?

Leo*_*bus 30

Xcode 9•Swift 4

extension Collection {
    func choose(_ n: Int) -> ArraySlice<Element> { shuffled().prefix(n) }
}
Run Code Online (Sandbox Code Playgroud)

游乐场测试

var alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
let shuffledAlphabet = alphabet.shuffled()  // "O", "X", "L", "D", "N", "K", "R", "E", "S", "Z", "I", "T", "H", "C", "U", "B", "W", "M", "Q", "Y", "V", "A", "G", "P", "F", "J"]
let letter = alphabet.randomElement()  // "D"
var numbers = Array(0...9)
let shuffledNumbers = numbers.shuffled()
shuffledNumbers                              // [8, 9, 3, 6, 0, 1, 4, 2, 5, 7]
numbers            // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers.shuffle() // mutate it  [6, 0, 2, 3, 9, 1, 5, 7, 4, 8]
numbers            // [6, 0, 2, 3, 9, 1, 5, 7, 4, 8]
let pick3numbers = numbers.choose(3)  // [8, 9, 2]
Run Code Online (Sandbox Code Playgroud)

Xcode 8.3.1•Swift 3.1

extension RangeReplaceableCollection {
    /// Returns a new Collection shuffled
    var shuffled: Self {
        var elements = self
        return elements.shuffledInPlace()
    }
    /// Shuffles this Collection in place
    @discardableResult
    mutating func shuffledInPlace() -> Self  {
        indices.dropLast().forEach {
            let subSequence = self[$0...$0]
            let distance = self.distance(from: startIndex, to: $0)
            let index = self.index(indices[..<self.index(endIndex, offsetBy: -distance)].randomElement()!, offsetBy: distance)
            replaceSubrange($0...$0, with: self[index...index])
            replaceSubrange(index...index, with: subSequence)
        }
        return self
    }
    func choose(_ n: Int) -> SubSequence { shuffled.prefix(n) }
}
Run Code Online (Sandbox Code Playgroud)


Cœu*_*œur 11

或者有没有人有更好/更优雅的解决方案?

我做.在算法上优于接受的答案,它arc4random_uniform为完全洗牌执行count-1 操作,我们可以简单地在n次操作中选择n个值. arc4random_uniform

实际上,我有两种方法比接受的答案做得更好:

好的解决方案

extension Array {
    /// Picks `n` random elements (straightforward approach)
    subscript (randomPick n: Int) -> [Element] {
        var indices = [Int](0..<count)
        var randoms = [Int]()
        for _ in 0..<n {
            randoms.append(indices.remove(at: Int(arc4random_uniform(UInt32(indices.count)))))
        }
        return randoms.map { self[$0] }
    }
}
Run Code Online (Sandbox Code Playgroud)

最佳方案

以下解决方案比前一个解决方案快两倍.

对于Swift 3.0和3.1

extension Array {
    /// Picks `n` random elements (partial Fisher-Yates shuffle approach)
    subscript (randomPick n: Int) -> [Element] {
        var copy = self
        for i in stride(from: count - 1, to: count - n - 1, by: -1) {
            let j = Int(arc4random_uniform(UInt32(i + 1)))
            if j != i {
                swap(&copy[i], &copy[j])
            }
        }
        return Array(copy.suffix(n))
    }
}
Run Code Online (Sandbox Code Playgroud)

对于Swift 3.2和4.x

extension Array {
    /// Picks `n` random elements (partial Fisher-Yates shuffle approach)
    subscript (randomPick n: Int) -> [Element] {
        var copy = self
        for i in stride(from: count - 1, to: count - n - 1, by: -1) {
            copy.swapAt(i, Int(arc4random_uniform(UInt32(i + 1))))
        }
        return Array(copy.suffix(n))
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

let digits = Array(0...9)  // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let pick3digits = digits[randomPick: 3]  // [8, 9, 0]
Run Code Online (Sandbox Code Playgroud)


iel*_*ani 5

您可以在数组上定义扩展:

extension Array {
    func pick(_ n: Int) -> [Element] {
        guard count >= n else {
            fatalError("The count has to be at least \(n)")
        }
        guard n >= 0 else {
            fatalError("The number of elements to be picked must be positive")
        }

        let shuffledIndices = indices.shuffled().prefix(upTo: n)
        return shuffledIndices.map {self[$0]}
    }
}

[ "Peter", "Steve", "Max", "Sandra", "Roman", "Julia" ].pick(3)
Run Code Online (Sandbox Code Playgroud)

如果初始数组可能有重复项,并且您希望值唯一:

extension Array where Element: Hashable {
    func pickUniqueInValue(_ n: Int) -> [Element] {
        let set: Set<Element> = Set(self)
        guard set.count >= n else {
            fatalError("The array has to have at least \(n) unique values")
        }
        guard n >= 0 else {
            fatalError("The number of elements to be picked must be positive")
        }

        return Array(set.prefix(upTo: set.index(set.startIndex, offsetBy: n)))
    }
}

[ "Peter", "Steve", "Max", "Sandra", "Roman", "Julia" ].pickUniqueInValue(3)
Run Code Online (Sandbox Code Playgroud)