dimanche 29 mars 2015

How can I generate a random number?

I tried



use std::rand::{task_rng, Rng};

fn main() {
// a number from [-40.0, 13000.0)
let num: f64 = task_rng().gen_range(-40.0, 1.3e4);
println!("{}", num);
}


but this gives



rust.rs:1:17: 1:25 error: unresolved import `std::rand::task_rng`. There is no `task_rng` in `std::rand`
rust.rs:1 use std::rand::{task_rng, Rng};
^~~~~~~~
error: aborting due to previous error


and I tried



extern crate rand;
use rand::Rng;

fn main() {
let mut rng = rand::thread_rng();
if rng.gen() { // random bool
println!("i32: {}, u32: {}", rng.gen::<i32>(), rng.gen::<u32>())
}
let tuple = rand::random::<(f64, char)>();
println!("{:?}", tuple)
}


and got



rust.rs:5:19: 5:35 error: unresolved name `rand::thread_rng`
rust.rs:5 let mut rng = rand::thread_rng();
^~~~~~~~~~~~~~~~
rust.rs:9:17: 9:44 error: unresolved name `rand::random`
rust.rs:9 let tuple = rand::random::<(f64, char)>();
^~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 2 previous errors


I am using Rust 1.0.0-dev.





Aucun commentaire:

Enregistrer un commentaire