random number in rust

To generate a random number in Rust, the standard library provides the rand crate which handles random number generation. Here's an example code snippet that generates a random number between 0 and 100:

main.rs
use rand::Rng;

fn main() {
    let mut rng = rand::thread_rng();
    let random_number = rng.gen_range(0, 101);
    println!("Random number: {}", random_number);
}
165 chars
8 lines

In the above code, rand::thread_rng() initializes a random number generator from the current thread, and then rng.gen_range(0, 101) generates a random number between 0 (inclusive) and 101 (exclusive). The random number is then printed to the console using a println! macro.

Note that you need to include the rand dependency in your Cargo.toml:

[dependencies]
rand = "0.8.4"
30 chars
3 lines

This should get you started with generating random numbers in Rust.

gistlibby LogSnag