I wanted to use some random numbers for a benchmark:
#![feature(test)]
/// benchmarks for different map implementation
extern crate test;
extern crate rand;
use test::Bencher;
use rand::Rng;
#[bench]
fn setup_random_hashmap(b: &mut Bencher) {
let mut val : u32 = 0;
let mut rng = rand::thread_rng();
let mut map = std::collections::HashMap::new();
b.iter(|| { map.insert(rng.gen::<u32>(), val); val += 1; })
}
However, rustc comes back with:
bench.rs:14:16: 14:32 error: unresolved name `rand::thread_rng`
bench.rs:14 let mut rng = rand::thread_rng();
thread_rng is defined in rand
as a pub fn
. What declaration am I missing? I am using 1.2.0-nightly (8f9f2fe97 2015-06-07)
.
You can reproduce this in Rust 1.0 (and at least up through 1.13):
extern crate rand;
fn main() {
rand::thread_rng();
}
With the same error:
error[E0425]: unresolved name `rand::thread_rng`
--> <anon>:4:5
|
4 | rand::thread_rng();
| ^^^^^^^^^^^^^^^^ unresolved name
To fix this, you need to add the rand
crate to your Cargo.toml:
[dependencies]
rand = "*"
This is due to the fact that Rust has an internal, hidden crate also called rand
. When the 1.0 stability guarantees were looming, many of these mostly-internal crates were spun out to their own repos and hosted on crates.io. However, the compiler still needed parts of these crates, and so stubs of them were left in the compiler, but marked private.
could I just use a different rng then?
A clever idea that appears to work! For future people, this only applies to nightly builds, and that means you accept whatever breaking code might happen!
#![feature(rand)]
extern crate rand;
use rand::Rng;
fn main() {
let mut rng = rand::IsaacRng::new_unseeded();
println!("{}", rng.gen_range(0, 10));
}
The biggest downside is that every run of the program will have the same sequence of numbers, because it will always have the same seed. This may be a benefit for benchmarking though!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With