Skip to content

Commit

Permalink
Make seedable types independent of one other
Browse files Browse the repository at this point in the history
  • Loading branch information
nvzqz committed May 4, 2017
1 parent 05dce46 commit 3caa9ff
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Sources/RandomKit/Types/RandomGenerator/ChaCha.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/// [1]: http://cr.yp.to/chacha.html
/// [2]: https://en.wikipedia.org/wiki/Salsa20#ChaCha_variant
/// [3]: https://doc.rust-lang.org/rand/rand/chacha/struct.ChaChaRng.html
public struct ChaCha: RandomBytesGenerator, SeedableFromRandomGenerator {
public struct ChaCha: RandomBytesGenerator, Seedable, SeedableFromRandomGenerator {

private typealias _State = _Array16<UInt32>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/// A generator that uses the [Mersenne Twister][MT] algorithm.
///
/// [MT]: https://en.wikipedia.org/wiki/Mersenne_Twister
public struct MersenneTwister: RandomBytesGenerator, SeedableFromRandomGenerator {
public struct MersenneTwister: RandomBytesGenerator, Seedable, SeedableFromRandomGenerator {

/// The number of `UInt64` values in a `_State`.
private static let _stateCount: Int = 312
Expand Down
13 changes: 2 additions & 11 deletions Sources/RandomKit/Types/RandomGenerator/Seedable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension Seedable {
}

/// A type that can be seeded by another `randomGenerator`.
public protocol SeedableFromRandomGenerator: Seedable, Random {
public protocol SeedableFromRandomGenerator: Random {

/// The default byte threshold at which `self` is reseeded in a `ReseedingRandomGenerator`.
static var reseedingThreshold: Int { get }
Expand Down Expand Up @@ -105,20 +105,11 @@ extension RandomGenerator where Self: SeedableFromRandomGenerator {

}

extension Seedable where Seed: Random {
extension Seedable where Self: SeedableFromRandomGenerator, Seed: Random {

/// Creates an instance seeded with `randomGenerator`.
public init<R: RandomGenerator>(seededWith randomGenerator: inout R) {
self.init(seed: Seed.random(using: &randomGenerator))
}

}

extension Seedable where Self: Random, Seed: Random {

/// Generates a random value of `Self` using `randomGenerator`.
public static func random<R: RandomGenerator>(using randomGenerator: inout R) -> Self {
return Self(seededWith: &randomGenerator)
}

}
2 changes: 1 addition & 1 deletion Sources/RandomKit/Types/RandomGenerator/Xoroshiro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
///
/// [1]: http://xoroshiro.di.unimi.it/
/// [2]: http://xoroshiro.di.unimi.it/xoroshiro128plus.c
public struct Xoroshiro: RandomBytesGenerator, SeedableFromRandomGenerator {
public struct Xoroshiro: RandomBytesGenerator, Seedable, SeedableFromRandomGenerator {

/// A default global instance seeded with `DeviceRandom.default`.
public static var `default` = seeded
Expand Down
2 changes: 1 addition & 1 deletion Sources/RandomKit/Types/RandomGenerator/Xorshift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
///
/// [1]: http://www.jstatsoft.org/v08/i14/paper
/// [2]: https://doc.rust-lang.org/rand/rand/struct.XorShiftRng.html
public struct Xorshift: RandomBytesGenerator, SeedableFromRandomGenerator {
public struct Xorshift: RandomBytesGenerator, Seedable, SeedableFromRandomGenerator {

/// A default global instance seeded with `DeviceRandom.default`.
public static var `default` = seeded
Expand Down
2 changes: 1 addition & 1 deletion Sources/RandomKit/Types/RandomGenerator/XorshiftStar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/// A generator that uses the [Xorshift1024*][1] algorithm.
///
/// [1]: http://xoroshiro.di.unimi.it/xorshift1024star.c
public struct XorshiftStar: RandomBytesGenerator, SeedableFromRandomGenerator {
public struct XorshiftStar: RandomBytesGenerator, Seedable, SeedableFromRandomGenerator {

/// The seed type.
public typealias Seed = (
Expand Down

0 comments on commit 3caa9ff

Please sign in to comment.