I have the following Rust code:
use rand::{rngs::StdRng, Rng, SeedableRng};
use std::convert::TryInto;
fn generate_random(bytes: Vec<u8>) -> u32 {
let bytes: [u8; 32] = bytes.try_into().unwrap();
let mut seeded_rng = StdRng::from_seed(bytes);
seeded_rng.gen_range(0..32)
}
This works fine for vectors of exact length 32 - however, if 32 is changed to some other number, such as 16 or 64, then there is a mismatched type error.
The rand
docs include an example for implementing SeedableRng
for RNGs with large seeds, however this is not a complete working example and requires implementing the rest of the RNG (i.e. traits Rng
and RngCore
) for a working solution.
Is it possible to create an RNG using an arbitrary-length vector as a seed without creating an entire RNG implementation myself, and if so, how do I go about doing this?
Aucun commentaire:
Enregistrer un commentaire