Reputation: 10730
I have a test program where I need to generate a random number. I therefore did a test comparing using
"uVal = rand::task_rng().gen();"
each time a random number is generated compared to creating an object using eg. :
let mut oRandom = std::rand::task_rng()
and generating multiple random numbers. Using the object (oRandom) created is much faster, so I thought I should pass the object to the function generating the random number, however I haven't been able to find a way to do that. It's not critical, but I presume it can be done.
Example 1 : not using the object : (much slower than 2)
let mut uVal : u8;
for _ in range(1, iMax) {
uVal = std::rand::task_rng().gen();
Example 2 : using the object : (much faster than 1)
let mut oRandom = std::rand::task_rng();
let mut uVal : u8;
for _ in range(1, iMax) {
uVal = oRandom.gen();
Example 3 : my attempt to pass the object to the function :
12 let mut oRandom = std::rand::task_rng();
13 fTest03(iMax, &mut oRandom);
53 fn fTest03(iMax : i64, oRandom : &mut std::rand::task_rng) {
This results in the following error :
test_rand003.rs:53:38: 53:57 error: use of undeclared type name
`std::rand::task_rng`
test_rand003.rs:53 fn fTest03(iMax : i64, oRandom : &mut std::rand::task_rng) {
How can I pass the variable "oRandom" in line 13 above to a function?
Upvotes: 1
Views: 1442
Reputation: 102126
There are two ways, either one can use the generic method, so it works with other RNGs, if you decide to stop using the task-local one:
fn fTest03<R: Rng>(iMax: i64, oRandom: &mut R) { ... }
Or, you can just use the return type of task_rng
directly:
fn fTest03(iMax: i64, oRandom: @mut IsaacRng) { ... }
(You may have to import Rng
/IsaacRng
, or fully-qualify them e.g. std::rand::Rng
.)
Either one should work with fTest03(10, std::rand::task_rng())
.
Upvotes: 1
Reputation: 1926
task_rng() is not a type, it's a function that returns a task local random number generator (TaskRng).
The signature of task_rng function is:
pub fn task_rng() -> @mut TaskRng
So if you change line 53 to:
fn fTest03(iMax : i64, oRandom : &mut std::rand::Rng) {...
things should work nicely.
Upvotes: 0