1

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?

Brian Oh
  • 9,604
  • 12
  • 49
  • 68
  • Did you [look at the docs](http://static.rust-lang.org/doc/0.8/std/rand/fn.task_rng.html) for the return type of `task_rng`? – huon Oct 30 '13 at 08:53
  • Some notes on standard Rust formatting: it (a) does not use Hungarian notation; (b) uses `snake_case` for variable names, not `camelCase`; (c) has no space before a colon in type definitions. Thus: `max: i64`, not `iMax : i64`. – Chris Morgan Oct 30 '13 at 11:41
  • Yes, I did read the documentation, and I have noted the snake_case etc. I'm just starting to learn Rust to see if I want to use it. I attempted many different things trying to get this to work, and posted just one. I thought this forum was for people to fast-track their learning, not to be beaten-up by you guys. Learning means one doesn't know much about it yet, and one cannot adopt everything at once. – Brian Oh Oct 30 '13 at 13:25
  • (The only reason I said that was because [you have asked other questions](http://stackoverflow.com/questions/19649005/why-using-rust-does-passing-a-mutable-struct-to-a-function-result-in-immutable-f) where you have demonstrated that you know about the distinction between types and functions that return those types, and wasn't sure if you'd established what type it was returning.) – huon Oct 30 '13 at 21:56
  • That's OK. People learn in different ways. I like to get something working and then figure out how it works. I think it's best to not analyze too much the motivation for questions. I may not be smart, but I'm not lazy. I can read between the lines (I think) when people respond and they don't like this and they don't like that about a question. I have questions that have received no response, and that's fine. I'd prefer that to being beaten over the head for not asking what they want in the way that they want it. "I ain't gonna work on Maggie's Farm No More" may result from too much pushing. – Brian Oh Oct 31 '13 at 01:23
  • @BrianOh I'm sorry, I was not intending to give that impression at all; I was just giving some hints on standard Rust formatting—as much for the benefit of future viewers as for you (because Stack Overflow is intended for answering questions multiple people will have and will be viewed later by others). Looking back at my comment, it does come across a bit curt, which was certainly not what I had intended. Please accept my apologies. – Chris Morgan Oct 31 '13 at 03:53
  • That's OK. No apology needed - I'm really not a dramatist. I'm sure we have the same goals in general. I'll try and comply. I do realize that you want other readers to use the correct standards. It is however also difficult to change hats all of the time between languages, but I guess I need to in this case so that others know the accepted way. So, please feel free to tell me the correct way without me over-reacting. I realize that it's "your" language and it's far better if everyone adopts the same standards. Just don't tell me I have to use eg. "a", "b", "c" for variable names. – Brian Oh Oct 31 '13 at 23:42

2 Answers2

1

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()).

huon
  • 94,605
  • 21
  • 231
  • 225
0

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.

Ercan Erden
  • 1,808
  • 1
  • 11
  • 8
  • 1
    This won't work directly... `@mut TaskRng` isn't a trait object while `&mut Rng` is using it as a trait object. – huon Oct 30 '13 at 21:56