71

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.

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.

// 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);
}

When I attempt to compile this, the following error results:

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);
                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

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.

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::<(f64, bool)>());
}

How can I generate a "simple" random number using Rust (e.g.: i64) within a given range (e.g.: 0 to n)?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Brian Oh
  • 9,604
  • 12
  • 49
  • 68

4 Answers4

99

This generates a random number between 0 (inclusive) and 100 (exclusive) using Rng::gen_range:

use rand::Rng; // 0.8.5

fn main() {
    // Generate random number in the range [0, 99]
    let num = rand::thread_rng().gen_range(0..100);
    println!("{}", num);
}

Don't forget to add the rand dependency to Cargo.toml:

[dependencies]
rand = "0.8"
BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
Manoel Stilpen
  • 1,219
  • 1
  • 10
  • 11
28

Editor's note: This answer is for a version of Rust prior to 1.0 and is not valid in Rust 1.0. See Manoel Stilpen's answer instead.

This has been changing a lot recently (sorry! it's all been me), and in Rust 0.8 it was called gen_integer_range (note the /0.8/ rather than /master/ in the URL, if you are using 0.8 you need to be reading those docs).

A word of warning: .gen_integer_range was entirely incorrect in many ways, the new .gen_range doesn't have incorrectness problems.


Code for master (where .gen_range works fine):

use std::rand::{task_rng, Rng};

fn main() {
    // a number from [-40.0, 13000.0)
    let num: f64 = task_rng().gen_range(-40.0, 1.3e4);
    println!("{}", num);
}
bric3
  • 40,072
  • 9
  • 91
  • 111
huon
  • 94,605
  • 21
  • 231
  • 225
  • I see that for 0.10 and master the documentation says to `use rand::Rng` http://static.rust-lang.org/doc/0.10/rand/index.html so it's a documentation bug probably – rofrol May 07 '14 at 16:33
  • 4
    I get `rust.rs:1:17: 1:25 error: unresolved import `std::rand::task_rng`. There is no `task_rng` in `std::rand` rust.rs:1 use std::rand::{task_rng, Rng}; ^~~~~~~~ error: aborting due to previous error ` when I try to compile it with `rustc 1.0.0-nightly (b4c965ee8 2015-03-02) (built 2015-03-03)` – Martin Thoma Mar 03 '15 at 13:42
  • 14
    For people who stumble upon this question now: `rand` seems to have become an independent crate on its own, and the answer by Manoel Stilpen below, where you explicitly use that crate, works. – xji Feb 18 '17 at 13:24
15

The documentation for Rng::gen_range states:

This function is optimised for the case that only a single sample is made from the given range. See also the Uniform distribution type which may be faster if sampling from the same range repeatedly.

Uniform can be used to generate a single value:

use rand::distributions::{Distribution, Uniform}; // 0.6.5

fn main() {
    let step = Uniform::new(0, 50);
    let mut rng = rand::thread_rng();
    let choice = step.sample(&mut rng);
    println!("{}", choice);
}

Playground

Or to generate an iterator of values:

use rand::distributions::{Distribution, Uniform}; // 0.6.5

fn main() {
    let step = Uniform::new(0, 50);
    let mut rng = rand::thread_rng();
    let choices: Vec<_> = step.sample_iter(&mut rng).take(10).collect();
    println!("{:?}", choices);
}

Playground

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
andy boot
  • 11,355
  • 3
  • 53
  • 66
  • In Rust 1.15.1, I'm getting "unresolved name" from `rand::thread_rng`. https://play.rust-lang.org/?gist=dfb901354368c0fb14bd82bda2435f95&version=stable&backtrace=0 – sudo Feb 20 '17 at 18:27
  • 1
    I think something changed since every answer and even the documentation uses `rand::thread_rng`. By the way, `rand` is unstable now, so you have to add `#![feature(rand)]` to the top of your file and use the nightly rustc. All I want to do is test something; I'm *this* close to just using the C `rand()` function through FFI and calling it a day. – sudo Feb 20 '17 at 18:34
  • This works for me with Rust 1.7.0. Have to add `rand = "0.3"` into `Cargo.toml` file though. – frabcus Apr 03 '17 at 12:32
  • 1
    As of rustc 1.42.0 (stable) with > rand 0.7 this appears to be the most accurate answer. – jaredwolff Mar 27 '20 at 21:52
-3

And why don't you just use a modulo?

let x = rng.gen<u32>() % 25

so your values will be between 0 and 24. Add an offset if you need a different range.

Flo
  • 421
  • 4
  • 6
  • 1
    This will give you numbers in range 0..25, but their distribution will not be uniform. It may be negligible with 25, since it is lot smaller than `u32::MAX`. But if you used this with range 0..4_000_000_000, you would find that you get the numbers in 0..294_649_297 twice as often as those in 294_649_297..4_000_000_000. – michalsrb Apr 19 '22 at 08:55