0

I have a look at the answers in regard to Swift2.0.1 in the website of stackoverflow. In spite of the usage of arc4random_uniform() , but it still could not be solved. Please help look at the lines of code as follows.

extension Array {
    mutating func shuffle() {
            for first in stride(from: self.count - 1, through: 1, by: -1) {
            let second = Int(arc4random_uniform(UInt32(first + 1)))
                      swap(&self[first], &self[second])
    }
}

Thanks in advance

Jongware
  • 22,200
  • 8
  • 54
  • 100
Mike Chen
  • 377
  • 3
  • 5
  • Working shuffle code for all Swift versions can be found at http://stackoverflow.com/a/24029847/1187415. – Martin R Jan 14 '17 at 15:53
  • After reviewing the similar issue again in Swift 2.0.1 in the website, I add the check line "if first != second { }", it works now. It seems that logic induction is correct. Thanks for the reminding. – Mike Chen Jan 14 '17 at 16:30
  • Martin, thanks for your direction. After I correct it, I go to the weblinks that you mentioned. It is a similar question exactly but have a lot of subtle answers. That will be quite helpful for an in-depth understanding for variant scenarios. – Mike Chen Jan 14 '17 at 18:02

0 Answers0