title: How can I generate a random number within a range in Rust?
body: <blockquote>
    <p>Editor's note: This code example is from a version of Rust prior to 1.0 and is not syntactically valid Rust 1.0 code. Updated versions of this code produce different errors, but the answers still contain valuable information.</p>
  </blockquote>
  
  <p>I came across the following example of how to generate a random number using Rust, but it doesn't appear to work. The example doesn't show which version of Rust it applies to, so perhaps it is out-of-date, or perhaps I got something wrong.</p>
  
  <pre><code>// http://static.rust-lang.org/doc/master/std/rand/trait.Rng.html
  
  use std::rand;
  use std::rand::Rng;
  
  fn main() {
      let mut rng = rand::task_rng();
      let n: uint = rng.gen_range(0u, 10);
      println!("{}", n);
      let m: float = rng.gen_range(-40.0, 1.3e5);
      println!("{}", m);
  }
  </code></pre>
  
  <p>When I attempt to compile this, the following error results:</p>
  
  <pre class="lang-none prettyprint-override"><code>test_rand002.rs:6:17: 6:39 error: type `@mut std::rand::IsaacRng` does not
  implement any method in scope named `gen_range`
  test_rand002.rs:6    let n: uint = rng.gen_range(0u, 10);
                                     ^~~~~~~~~~~~~~~~~~~~~~
  test_rand002.rs:8:18: 8:46 error: type `@mut std::rand::IsaacRng` does not
  implement any method in scope named `gen_range`
  test_rand002.rs:8    let m: float = rng.gen_range(-40.0, 1.3e5);
                                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  </code></pre>
  
  <p>There is another example (as follows) on the same page (above) that does work. However, it doesn't do exactly what I want, although I could adapt it.</p>
  
  <pre><code>use std::rand;
  use std::rand::Rng;
  
  fn main() {
      let mut rng = rand::task_rng();
      let x: uint = rng.gen();
      println!("{}", x);
      println!("{:?}", rng.gen::&lt;(f64, bool)&gt;());
  }
  </code></pre>
  
  <p>How can I generate a "simple" random number using Rust (e.g.: <code>i64</code>) within a given range (e.g.: 0 to n)?</p>