Skip to content

Instantly share code, notes, and snippets.

@MartinJNash
Created April 26, 2015 10:00

Revisions

  1. MartinJNash created this gist Apr 26, 2015.
    21 changes: 21 additions & 0 deletions Array+Shuffle.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@

    extension Array {

    var shuffled: Array<T> {
    var newer = self
    newer.shuffle()
    return newer
    }

    mutating func shuffle() {
    let count = self.count
    if count > 1 {
    for x in 0..<count {
    let idx = Int(arc4random_uniform(UInt32(x+1)))
    swap(&self[x], &self[idx])
    }
    }
    }


    }