Hello!
I'm currently trying to port my old and trusty pseudo-random number generation code from C to Rust.
It's just a simple implementation of the Xorshift128+ algorithm, and the algorithm it self was quite easy to implement in rust, but now I'm stuck when trying to implement some "quality of life" functionality for the number generator.
My ambition was to implement a generic function that can generate a random number of any basic type simply by interpreting the resulting u64
as another type, just as i could do in C.
But I can't find a way to do this, and I'm starting to think that this is not allowed in Rust for safety reasons.
Here's an example of the closest I have gotten to solving this problem:
Signature of randomization function:
fn xor_shift_128_plus(&mut self) -> u64
generation function:
pub fn generate<T: From<u64>>(&mut self) -> T {
self.xor_shift_128_plus().into()
}
And this works fine when running the following code:
fn main() {
let mut prng = Prng::new();
prng.seed(123);
println!("{}", prng.generate::<u64>());
}
Output: 16227247910015008063
But if I were to change the call to:
prng.generate::<u32>()
the program wont compile because u32
does not implement From<u64>
.
So the problem seems to be that Rust does not allow for casting from a bigger to a smaller type, in respect to bit-size.
Now, I can't really see why it is problematic to do this operation but I'm sure there are good reasons for not allowing this by default, but surely there must be a way to allow for this type of truncating cast?