rust random float between 0 and 1
You can generate a random float between 0 and 1 in Rust by using the rand
crate. Here's an example code snippet:
use rand::Rng;
fn main() {
let mut rng = rand::thread_rng();
let random_float: f64 = rng.gen_range(0.0..1.0);
println!("{}", random_float);
}
This code imports the rand::Rng
trait, which provides methods for generating random values. It then creates a new random number generator using rand::thread_rng()
. The gen_range
method is used to generate a random float between 0 and 1, and it takes a range as an argument (0.0..1.0
in this case). Finally, the resulting random float is printed to the console.