Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate a random number within a range in Rust?

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)?

like image 252
Brian Oh Avatar asked Oct 30 '13 00:10

Brian Oh


People also ask

How do you generate a random number from within a range?

Method 1: Using Math. random() function is used to return a floating-point pseudo-random number between range [0,1) , 0 (inclusive) and 1 (exclusive). This random number can then be scaled according to the desired range.

How do you make a random number generator in Rust?

Generate random numbers from an array by shuffling the array: let mut rng = WyRand::new(); let mut items = vec![ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; rng. shuffle(&mut items); println!(

How do you use random numbers in Rust?

To get you started quickly, the easiest and highest-level way to get a random value is to use random() ; alternatively you can use thread_rng() .

How do you generate random numbers between ranges in CPP?

How to Generate Random Numbers in C++ Within a Range. Similar to 1 and 10, you can generate random numbers within any range using the modulus operator. For instance, to generate numbers between 1 and 100, you can write int random = 1+ (rand() % 100).


2 Answers

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

use rand::Rng; // 0.8.0  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" 
like image 131
Manoel Stilpen Avatar answered Oct 26 '22 12:10

Manoel Stilpen


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); } 
like image 30
huon Avatar answered Oct 26 '22 12:10

huon