-
Notifications
You must be signed in to change notification settings - Fork 92
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,9 @@ public protocol Seedable { | |
/// The seed type. | ||
associatedtype Seed | ||
|
||
/// Current seed value. | ||
var seed: Seed { get } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Requiring |
||
|
||
/// Creates an instance from `seed`. | ||
init(seed: Seed) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better if |
||
return _state | ||
} | ||
|
||
/// A default global instance seeded with `DeviceRandom.default`. | ||
public static var `default` = seeded | ||
|
@@ -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 | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return (x, y, z, w) | ||
} | ||
|
||
/// A default global instance seeded with `DeviceRandom.default`. | ||
public static var `default` = seeded | ||
|
@@ -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 | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -38,6 +37,11 @@ public struct XorshiftStar: RandomBytesGenerator, Seedable, SeedableFromRandomGe | |
UInt64, UInt64, UInt64, UInt64 | ||
) | ||
|
||
/// The seed value. | ||
public var seed: Seed { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to with |
||
return _state | ||
} | ||
|
||
private typealias _State = _Array16<UInt64> | ||
|
||
private static var _jump: _State = ( | ||
|
There was a problem hiding this comment.
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.