Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
洗牌阵法swift 3_Swift_Swift3 - Fatal编程技术网

洗牌阵法swift 3

洗牌阵法swift 3,swift,swift3,Swift,Swift3,如何将下面的函数转换为swift 3?当前获得的是一个二进制运算符“…我建议简单地洗牌数组,而不是尝试将其扩展到集合: extension Array { mutating func shuffle () { for i in (0..<self.count).reversed() { let ix1 = i let ix2 = Int(arc4random_uniform(UInt32(i+1)))

如何将下面的函数转换为
swift 3
?当前获得的是一个
二进制运算符“…我建议简单地洗牌数组,而不是尝试将其扩展到集合:

extension Array {
    mutating func shuffle () {
        for i in (0..<self.count).reversed() {
            let ix1 = i
            let ix2 = Int(arc4random_uniform(UInt32(i+1)))
            (self[ix1], self[ix2]) = (self[ix2], self[ix1])
        }
    }
}
扩展数组{
变异func shuffle(){

对于i in(0..
计数
返回一个
索引距离
,它是描述 两个集合索引之间的距离。
IndexDistance
为 必须是
签名整数
,但不必是
整数
,并且可以 不同于
索引
。因此无法创建
范围
0..Gamekit中有一个fisher-yates洗牌:

import GameKit
let unshuffledArray = [1,2,3,4]
let shuffledArray = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: unshuffledArray)
print(shuffledArray)
您还可以传入并存储一个随机种子,因此每次提供相同的种子时,您都会获得相同的伪随机随机洗牌值序列,以防需要重新创建模拟

import GameKit
let unshuffledArray = [1,2,3,4]
let randomSource = GKLinearCongruentialRandomSource(seed: 1)
let shuffledArray = randomSource.arrayByShufflingObjects(in: unshuffledArray)
//Always [1,4,2,3]
print(shuffledArray)

您可以使用GameplayKit框架中的NSArray扩展来实现以下目的:

import GameplayKit

extension Collection {
    func shuffled() -> [Iterator.Element] {
        let shuffledArray = (self as? NSArray)?.shuffled()
        let outputArray = shuffledArray as? [Iterator.Element]
        return outputArray ?? []
    }
    mutating func shuffle() {
        if let selfShuffled = self.shuffled() as? Self {
            self = selfShuffled
        }
    }
}

// Usage example:

var numbers = [1,2,3,4,5]
numbers.shuffle()

print(numbers) // output example: [2, 3, 5, 4, 1]

print([10, "hi", 9.0].shuffled()) // output example: [hi, 10, 9]

我只是觉得我应该指出,
让j
中的
+I
可能应该是
+startIndex
。否则这很可能会导致索引越界。@bjareh.Søndergaard:我相当肯定上面的代码是正确的,并且我已经用数组和数组片对它进行了测试。
I
正在运行中ge
startIndex..=startIndex
j
。啊,我现在知道我把你的代码读错了。对不起,现在还是凌晨。我把你的j读成了
endIndex-1
而不是
endIndex-i
)在我看来,startIndex..@MarkDail:如果
i==endIndex-1
那么
j==i
将是唯一可以交换的索引,因此如果
i
运行到
endIndex-2
问题的可能重复项已过时,就足够了,因为Swift 3的参考已更新。
extension MutableCollection {
    /// Shuffle the elements of `self` in-place.
    mutating func shuffle() {
        for i in indices.dropLast() {
            let diff = distance(from: i, to: endIndex)
            let j = index(i, offsetBy: numericCast(arc4random_uniform(numericCast(diff))))
            swapAt(i, j)
        }
    }
}
import GameKit
let unshuffledArray = [1,2,3,4]
let shuffledArray = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: unshuffledArray)
print(shuffledArray)
import GameKit
let unshuffledArray = [1,2,3,4]
let randomSource = GKLinearCongruentialRandomSource(seed: 1)
let shuffledArray = randomSource.arrayByShufflingObjects(in: unshuffledArray)
//Always [1,4,2,3]
print(shuffledArray)
import GameplayKit

extension Collection {
    func shuffled() -> [Iterator.Element] {
        let shuffledArray = (self as? NSArray)?.shuffled()
        let outputArray = shuffledArray as? [Iterator.Element]
        return outputArray ?? []
    }
    mutating func shuffle() {
        if let selfShuffled = self.shuffled() as? Self {
            self = selfShuffled
        }
    }
}

// Usage example:

var numbers = [1,2,3,4,5]
numbers.shuffle()

print(numbers) // output example: [2, 3, 5, 4, 1]

print([10, "hi", 9.0].shuffled()) // output example: [hi, 10, 9]