Last active
November 13, 2017 11:01
-
-
Save Azoy/15f0518df38df9b722d4cb17bafea4c1 to your computer and use it in GitHub Desktop.
Swift Random Unification Design
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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