Skip to content

Instantly share code, notes, and snippets.

@Azoy Azoy/Random.swift
Last active Nov 13, 2017

Embed
What would you like to do?
Swift Random Unification Design
public protocol RandomSource {
static var shared: RandomSource { get }
func next<T : FixedWidthInteger>(_ type: T.Type) -> T
func nextUniform<T : BinaryFloatingPoint>(_ type: T.Type) -> T
}
// Utilizes /dev/urandom
public class DevRandom: RandomSource {
public static let shared = DevRandom()
public func next<T : FixedWidthInteger>(_ type: T.Type) -> T {
}
public func nextUniform<T : BinaryFloatingPoint>(_ type: T.Type) -> T {
}
}
public protocol UnsafeRandomSource {
init<T : Numeric>(seed: T)
}
public class XoroshiroRandom: RandomSource, UnsafeRandomSource {
public static let shared = XoroshiroRandom(seed: Int.random)
public init<T : Numeric>(seed: T) {
}
public func next<T : FixedWidthInteger>(_ type: T.Type) -> T {
}
public func nextUniform<T : BinaryFloatingPoint>(_ type: T.Type) -> T {
}
}
extension RandomAccessCollection {
public var random: Element {
return random(using: DevRandom.shared)
}
public func random(using source: RandomSource) -> Element {
}
}
extension Range where Bound : BinaryFloatingPoint {
public var random: Bound {
return random(using: DevRandom.shared)
}
public func random(using source: RandomSource) -> Bound {
}
}
extension ClosedRange where Bound : BinaryFloatingPoint {
public var random: Bound {
return random(using: DevRandom.shared)
}
public func random(using source: RandomSource) -> Bound {
}
}
public protocol Randomizable {
static var random: Self
static func random(using souce: RandomSource) -> Self
}
extension Randomizable {
public static var random: Self {
return random(using: DevRandom.shared)
}
}
extension FixedWidthInteger: Randomizable {
public static func random(using source: RandomSource) -> Self {
return (0 ..< Self.max).random(using: source)
}
}
extension BinaryFloatingPoint: Randomizable {
public static func random(using source: RandomSource) -> Self {
return (0 ... 1.0).random(using: source)
}
}
// Save this for a separate proposal, but introduce the link between the two proposals
extension RandomAccessCollection {
public func shuffled(using source: RandomSource = DevRandom.shared) -> Self {
}
}
extension MutableCollection {
public mutating func shuffle(using source: RandomSource = DevRandom.shared) {
}
}
print((0 ..< 10).random)
print((0 ..< 1.0).random)
let greetings = ["hi", "hello", "hola", "hey"]
print(greetings.random(using: XoroshiroRandom.shared))
let customXoro = XoroshiroRandom(seed: 16)
print(greetings.random(using: customXoro))
print(customXoro.next(UInt8.self))
print(Int.random) // (0 ..< Int.max).random
print(Double.random(using: customXoro)) // (0 ... 1.0).random(using: customXoro)
// Save this for a separate proposal, but introduce the link between the two proposals
print(greetings.shuffled()) // ["hello", "hey", "hi", "hola"] an example output
var numbers = Array(0 ..< 5)
numbers.shuffle(using: customXoro)
print(numbers) // [1, 4, 2, 0, 3] an example output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.