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.rs216 chars10 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