get a random number from a normal distribution with a mean of 0 and a standard deviation of 1 in rust

To get a random number from a normal distribution with a mean of 0 and a standard deviation of 1 in rust, you can use the rand crate. Here's an example code snippet that shows how to generate a random number from a standard normal distribution:

main.rs
extern crate rand;

use rand::distributions::{Distribution, Normal};

fn main() {
    let mean = 0.0;
    let std_dev = 1.0;

    let normal = Normal::new(mean, std_dev);
    let mut rng = rand::thread_rng();

    let num = normal.sample(&mut rng);
    println!("Random number: {}", num);
}
291 chars
15 lines

The Normal::new(mean, std_dev) creates a normal distribution object with the specified mean and standard deviation. The rng variable is a random number generator provided by the rand crate. Finally, the sample method is used on the normal distribution object to generate a random number from that distribution.

The above code will print a random number from the standard normal distribution to the console. You can modify the mean and std_dev variables as desired to generate random numbers from other normal distributions.

gistlibby LogSnag