SeedableRng
has an example, but there is no explanation for what the seed is. Searching for the Seed
type does not provide details. Searching in the GitHub source directory also did not clear this up for me.
let seed: &[_] = &[1, 2, 3, 4];
let mut rng: StdRng = SeedableRng::from_seed(seed);
Does seed
have to be a four element array of integers? Why? Can it be anything else?
Seed
can be any type that the implementer of the trait decides, it's a generic type defined as part of the trait itself:
pub trait SeedableRng<Seed>: Rng {
// ^^^^
fn reseed(&mut self, _: Seed);
fn from_seed(seed: Seed) -> Self;
}
Isaac64Rng
uses a slice of u64
:
impl<'a> SeedableRng<&'a [u64]> for Isaac64Rng
// ^^^^^^^^^
XorShiftRng
uses exactly 4 u32
:
impl SeedableRng<[u32; 4]> for XorShiftRng
// ^^^^^^^^
You can see all the implementers of SeedableRng
from inside the rand crate in its documentation:
impl<S, R: SeedableRng<S>, Rsdr: Reseeder<R> + Default> SeedableRng<(Rsdr, S)> for ReseedingRng<R, Rsdr>
impl<'a> SeedableRng<&'a [u32]> for ChaChaRng
impl<'a> SeedableRng<&'a [u32]> for IsaacRng
impl<'a> SeedableRng<&'a [u64]> for Isaac64Rng
impl SeedableRng<[u32; 4]> for XorShiftRng
impl<'a> SeedableRng<&'a [usize]> for StdRng
It's also possible for other crates to define random number generators with different seed types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With