Swiftpack.co -  Package - glessard/shuffle
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Shuffle a Collection
.package(url: "https://github.com/glessard/shuffle.git", from: "2.3.0")

Shuffle Build Status

Adds a shuffled() method to any Collection, and a shuffle() method to any MutableCollection-- for Swift prior to 4.2

It shuffles the Collection as lazily as possible, without mutating the collection. The collection's indices are copied to an array, which in turn gets shuffled.

This allows the input collection to be indexed only. In general this should minimize memory allocation, for example in cases where the size of Element is greater than the size of Index.

For Swift 4.2 and up, the standard library's Collection.shuffle() and Collection.randomElement() will perform much better. While it may be possible to craft examples where using the approach in this package manages to perform better, they are undoubtedly few and far between.


import Shuffle

let a = Array(0...4)

var stats = Array<Array<Int>>(repeating: Array(repeating: 0, count: a.count), count: a.count)

(0..<8000).forEach {
  _ in
  for (i,j) in a.shuffled().enumerated()  // shuffled() used here
    stats[i][j] += 1

var sums = Array<Int>(repeating: 0, count: a.count)
for total in stats
  let t = total.reduce(0, +)
  for i in 0..<sums.count
    sums[i] += total[i]
  print(total, t)


Stars: 3
Last commit: 5 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco