Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the possible types/values for the SeedableRng Seed?

Tags:

rust

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?

like image 864
Roger Allen Avatar asked Mar 17 '18 23:03

Roger Allen


1 Answers

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.

like image 181
Shepmaster Avatar answered Nov 15 '22 10:11

Shepmaster