[![rand-badge]][rand] [![cat-science-badge]][cat-science]
用Rng::gen_range
,在半开放[0, 10)
范围(不包括10
)生成随机值。
extern crate rand;
use rand::Rng;
fn main() {
let mut rng = rand::thread_rng();
println!("Integer: {}", rng.gen_range(0, 10));
println!("Float: {}", rng.gen_range(0.0, 10.0));
}
Uniform
可以用来获得均匀分布的值。下面的代码是相同作用,但是当在相同范围内,重复生成数字时可能更快。
extern crate rand;
use rand::distributions::{Distribution, Uniform};
fn main() {
let mut rng = rand::thread_rng();
let die = Uniform::from(1..7);
loop {
let throw = die.sample(&mut rng);
println!("Roll the die: {}", throw);
if throw == 6 {
break;
}
}
}