Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revealing current seed as a public read-only property #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Sources/RandomKit/Types/RandomGenerator/ChaCha.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,19 @@
/// [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, Seedable, SeedableFromSequence, SeedableFromRandomGenerator {

/// The seed type.
public typealias Seed = [UInt32]

/// The seed value
public var seed: Seed {
Copy link
Owner

@nvzqz nvzqz Oct 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless ChaCha(seed: rng.seed).seed == rng.seed, this should be removed since it may not be valid to consider this as a seed. Please add a test to ensure this invariant is met.

return [
_state.0, _state.1, _state.2, _state.3,
_state.4, _state.5, _state.6, _state.7,
_state.8, _state.9, _state.10, _state.11,
_state.12, _state.13, _state.14, _state.15,
]
}

/// The seed sequence's element type.
public typealias SeedSequenceElement = UInt32

Expand Down
4 changes: 4 additions & 0 deletions Sources/RandomKit/Types/RandomGenerator/MersenneTwister.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
///
/// [MT]: https://en.wikipedia.org/wiki/Mersenne_Twister
public struct MersenneTwister: RandomBytesGenerator, Seedable, SeedableFromRandomGenerator {
/// The seed value.
public var seed: UInt64 {
return _state.0
}

/// The number of `UInt64` values in a `_State`.
private static let _stateCount: Int = 312
Expand Down
3 changes: 3 additions & 0 deletions Sources/RandomKit/Types/RandomGenerator/Seedable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public protocol Seedable {
/// The seed type.
associatedtype Seed

/// Current seed value.
var seed: Seed { get }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requiring Seedable to have a seed property is a breaking change since types outside of this project can conform to this protocol and would have to update their code accordingly.


/// Creates an instance from `seed`.
init(seed: Seed)

Expand Down
11 changes: 9 additions & 2 deletions Sources/RandomKit/Types/RandomGenerator/Xoroshiro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
/// [1]: http://xoroshiro.di.unimi.it/
/// [2]: http://xoroshiro.di.unimi.it/xoroshiro128plus.c
public struct Xoroshiro: RandomBytesGenerator, Seedable, SeedableFromRandomGenerator {
/// The seed type.
public typealias Seed = (UInt64, UInt64)

/// The seed value.
public var seed: Seed {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better if _state was made public and mutable as state instead. See my comment about Seedable.

return _state
}

/// A default global instance seeded with `DeviceRandom.default`.
public static var `default` = seeded
Expand All @@ -54,10 +61,10 @@ public struct Xoroshiro: RandomBytesGenerator, Seedable, SeedableFromRandomGener
public static var defaultReseeding = reseeding

/// The internal state.
private var _state: (UInt64, UInt64)
private var _state: Seed

/// Creates an instance from `seed`.
public init(seed: (UInt64, UInt64)) {
public init(seed: Seed) {
_state = seed
}

Expand Down
9 changes: 8 additions & 1 deletion Sources/RandomKit/Types/RandomGenerator/Xorshift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
/// [1]: http://www.jstatsoft.org/v08/i14/paper
/// [2]: https://doc.rust-lang.org/rand/rand/struct.XorShiftRng.html
public struct Xorshift: RandomBytesGenerator, Seedable, SeedableFromRandomGenerator {
/// The seed type.
public typealias Seed = (UInt32, UInt32, UInt32, UInt32)

/// The seed value.
public var seed: Seed {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x, y, z, and y can probably be made into a public mutable state variable which can be exposed instead.

return (x, y, z, w)
}

/// A default global instance seeded with `DeviceRandom.default`.
public static var `default` = seeded
Expand All @@ -49,7 +56,7 @@ public struct Xorshift: RandomBytesGenerator, Seedable, SeedableFromRandomGenera
/// Creates an instance from `seed`.
///
/// - precondition: `seed` is nonzero.
public init(seed: (UInt32, UInt32, UInt32, UInt32)) {
public init(seed: Seed) {
(x, y, z, w) = seed
}

Expand Down
6 changes: 5 additions & 1 deletion Sources/RandomKit/Types/RandomGenerator/XorshiftStar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
///
/// [1]: http://xoroshiro.di.unimi.it/xorshift1024star.c
public struct XorshiftStar: RandomBytesGenerator, Seedable, SeedableFromRandomGenerator {

/// The seed type.
public typealias Seed = (
UInt64, UInt64, UInt64, UInt64,
Expand All @@ -38,6 +37,11 @@ public struct XorshiftStar: RandomBytesGenerator, Seedable, SeedableFromRandomGe
UInt64, UInt64, UInt64, UInt64
)

/// The seed value.
public var seed: Seed {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to with Xoroshiro, should probably change _state to be public and mutable instead.

return _state
}

private typealias _State = _Array16<UInt64>

private static var _jump: _State = (
Expand Down