Swift implementation of a fast, space-efficient, and statistically excellent 64-bit random number generator using a PCG algorithm. Works as a drop in replacement to the default random number generator in Swift 4.2+.
Requires Swift 4.2+
The Swift Package Manager is a decentralized dependency manager for Swift.
-
Add the project to your
Package.swift
.import PackageDescription let package = Package( name: "MyProject", dependencies: [ .Package(url: "https://github.com/amarcu5/pcg-swift.git", majorVersion: 1) ] )
-
Import the PermutedCongruentialGenerator module.
import PermutedCongruentialGenerator
var pcg = PermutedCongruentialGenerator.shared
["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"].randomElement(using: &pcg)!
// "Wed"
["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"].shuffled(using: &pcg)
// ["Tue", "Mon", "Fri", "Thu", "Sun", "Sat", "Wed"]
Int.random(in: 1...6, using: &pcg)
// 5
Double.random(in: -1...1, using: &pcg)
// -0.5793378414800663