get a normally distributed random number in rust

To generate a normally distributed random number in Rust, we can use the rand_distr crate. Here's an example that generates a random number with a mean of 0 and standard deviation of 1:

main.rs
use rand::prelude::*;
use rand_distr::Normal;

fn main() {
    let mut rng = thread_rng();
    let normal = Normal::new(0.0, 1.0).unwrap();
    let num = rng.sample(normal);
    println!("Random number: {}", num);
}
216 chars
10 lines

In this example, we first create a ThreadRng using the thread_rng function from the rand crate. Then, we create a Normal distribution with mean 0 and standard deviation 1 using the Normal::new function from the rand_distr crate. Finally, we generate a random number using the sample method of the ThreadRng and Normal objects.

Note that the rand_distr crate is not included in the standard library and must be added to your Cargo.toml dependencies.

gistlibby LogSnag