revealing current seed as a public read-only property - #44
Conversation
| associatedtype Seed | ||
| /// Current seed value. | ||
| var seed: Seed { get } |
There was a problem hiding this comment.
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.
| public typealias Seed = (UInt64, UInt64) | ||
| /// The seed value. | ||
| public var seed: Seed { |
There was a problem hiding this comment.
It would be better if _state was made public and mutable as state instead. See my comment about Seedable.
| public typealias Seed = (UInt32, UInt32, UInt32, UInt32) | ||
| /// The seed value. | ||
| public var seed: Seed { |
There was a problem hiding this comment.
x, y, z, and y can probably be made into a public mutable state variable which can be exposed instead.
| ) | ||
| /// The seed value. | ||
| public var seed: Seed { |
There was a problem hiding this comment.
Similar to with Xoroshiro, should probably change _state to be public and mutable instead.
| public typealias Seed = [UInt32] | ||
| /// The seed value | ||
| public var seed: Seed { |
There was a problem hiding this comment.
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.
Side note: this makes me wish Swift had more of Rust's abilities regarding protocols/traits. How I'd implement pubtraitSeedable<S>{fnfrom_seed(seed:S) -> SelfwhereSelf:Sized;fnreseed(&mutself,seed:S);}Then there would be no need for impl<S:IntoIterator<Item=u64>>Seedable<S>forChaCha{defaultfnfrom_seed(seed:S) -> ChaCha{for s in seed.into_iter(){/* ... */}}defaultfnreseed(&mutself,seed:S){for s in seed.into_iter(){/* ... */}}}impl<S>Seedable<S>forChaChawhereS:for<'a>IntoIterator<Item=&'au64>{defaultfnfrom_seed(seed:S) -> ChaCha{// Dereference the integerlet iter = seed.map(|&s| s);ChaCha::from_seed(iter)}defaultfnreseed(&mutself,seed:S){// Dereference the integerlet iter = seed.map(|&s| s);self.reseed(iter)}}Specialization would allow for: implSeedable<u64>forChaCha{fnfrom_seed(seed:u64) -> ChaCha{ChaCha::from_seed(&[seed])}fnreseed(&mutself,seed:u64){self.reseed(&[seed])}}Edit: apparently because of problems with specialization, this implementation wouldn't be currently possible in Rust either 😢 https://play.rust-lang.org/?gist=409dba7bc6162833285c8437815bc178&version=nightly |
Sometimes it might be helpful to be able to log seed that was used to create generator.
So we can reproduce same random sequence using that seed we know.
For example it can be logged for test suit session whose data is generated randomly.
And if some tests fail, we can fix them and re-run test suit using that seed to verify it on the same 'random' data.