洗牌阵法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
正在运行中gestartIndex..=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]