Gen_range and 1 or 2 arguments

Hello everybody,
I'm new with rust, and I'm trying some exercises, at the begining only copying and testing.

I would like to get a random number (interger or float, whatever, it is a testing purpose)
In cargo.toml, I added:

[dependencies]
rand = "0.8.0"

And in the main.rs:

extern crate rand;
use rand::thread_rng;
use rand::Rng;

fn main() {
	
	let mut rng = thread_rng();
    let y: f64 = rng.gen_range(-10.0, 10.0);
    println!("Number from -10. to 10.: {}", y);
    println!("Number from 0 to 9: {}", rng.gen_range(0, 10));
}

But I get:

error[E0061]: this function takes 1 argument but 2 arguments were supplied
  --> src/main.rs:57:22
   |
57 |     let y: f64 = rng.gen_range(-10.0, 10.0);
   |                      ^^^^^^^^^ -----  ---- supplied 2 arguments
   |                      |
   |                      expected 1 argument

error[E0061]: this function takes 1 argument but 2 arguments were supplied
  --> src/main.rs:59:44
   |
59 |     println!("Number from 0 to 9: {}", rng.gen_range(0, 10));
   |                                            ^^^^^^^^^ -  -- supplied 2 arguments
   |                                            |
   |                                            expected 1 argument

Do you have any idea why I cannot write 2 numbers for my range ? Thank you.

In the latest version of rand you write rng.gen_range(-10.0..10.0) instead, using Rust's range syntax.

2 Likes

Thank you very much !
Now, the syntax have changed, and should be:

    let mut rng = thread_rng();
    let y: f64 = rng.gen_range(-10.0..10.0);
    println!("Number from -10. to 10.: {}", y);
    println!("Number from 0 to 9: {}", rng.gen_range(0..10));
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.