Search code examples
rusttraitstrait-objects

Is there a way to store a random number generator as a trait object?


Is there a way to hold a generic random number generator in Rust? I'd like a way to write a generic piece of code such as:

use rand::Rng; // 0.7.2

fn main() {
    // Purely random numbers on the interval 0 to 1
    println!("Pure random");
    let mut rng: Box<dyn rand::Rng> = Box::new(rand::thread_rng());
    for i in 0..10 {
        println!("{}", rng.gen::<f64>());
    }
    println!("");

    // On a seed
    *rng = rand::SeedableRng::seed_from_u64(0);
    for i in 0..10 {
        println!("{}", rng.gen::<f64>());
    }
    println!("");
}

The variable rng holds different kinds of random number generators using a seed or otherwise. However, there's a whole host of errors with this code such as:

error[E0038]: the trait `rand::Rng` cannot be made into an object
 --> src/main.rs:6:18
  |
6 |     let mut rng: Box<dyn rand::Rng> = Box::new(rand::thread_rng());
  |                  ^^^^^^^^^^^^^^^^^^ the trait `rand::Rng` cannot be made into an object
  |
  = note: method `gen` has generic type parameters
  = note: method `gen_range` has generic type parameters
  = note: method `sample` has generic type parameters
  = note: method `fill` has generic type parameters
  = note: method `try_fill` has generic type parameters

As such, what's the correct way to hold a random number generator so that it can be replaced with another? Basically, I would like to generate purely random numbers, but then replace the generator with one using a seed for debugging purposes.


Solution

  • You want to use RngCore for your trait object, not Rng:

    use rand::{Rng, RngCore, SeedableRng}; // 0.7.2
    use rand_chacha::ChaCha20Rng; // 0.2.1
    
    fn main() {
        let mut rng: Box<dyn RngCore>;
    
        rng = Box::new(rand::thread_rng());
        for _ in 0..10 {
            println!("{}", rng.gen::<f64>());
        }
    
        println!();
    
        rng = Box::new(ChaCha20Rng::seed_from_u64(42));
        for _ in 0..10 {
            println!("{}", rng.gen::<f64>());
        }
    }
    

    See also:

    Other points

    • rand::SeedableRng::seed_from_u64 doesn't make any sense because you have never specified a concrete type to implement the trait object.

    • Purely random numbers

      Both generators are pseudo-random number generators and produce the same kind of numbers; one isn't "more random" than the other.