rustc 1.0.0-nightly (be9bd7c93 2015-04-05) (built 2015-04-05)
extern crate rand;
fn test(a: isize) {
println!("{}", a);
}
fn main() {
test(rand::random() % 20)
}
this code compiled before Rust beta, but now it doesn't:
src/main.rs:8:10: 8:22 error: unable to infer enough type information about `_`; type annotations required [E0282]
src/main.rs:8 test(rand::random() % 20)
^~~~~~~~~~~~
I must write this code to compile:
extern crate rand;
fn test(a: isize) {
println!("{}", a);
}
fn main() {
test(rand::random::<isize>() % 20)
}
How can I make the compiler infer the type?
random()
and then% 20
to get a number between0
and20
, but arand::distributions::Range
(see example doc.rust-lang.org/rand/rand/… ). The issue with using the modulo operator is, that you don't get a truly random distribution. You are instead increasing the probability for small numbers. – oli_obk