其他分享
首页 > 其他分享> > Rust Rand生成随机数

Rust Rand生成随机数

作者:互联网

# in project file
cargo add rand
extern crate rand;
use rand::Rng;
fn main() {
let mut rng = rand::thread_rng();

// an unbiased integer over the entire range:
let i: i32 = rng.gen();
println!("i = {i}");

// a uniformly distributed value between 0 and 1:
let x: f64 = rng.gen();
println!("x = {x}");

// simulate rolling a die:
// 生成指定范围的随机数
println!("roll = {}", rng.gen_range(1..=6));
}

标签:Rand,rand,rng,range,let,随机数,println,gen,Rust
来源: https://www.cnblogs.com/pphboy/p/16462753.html