Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Random module

cargo new show-random
cd show-random
cargo add random
[package]
name = "show-random"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.8.5"
fn main() {
    {
        let random_bool: bool = rand::random();
        println!("random_bool: {random_bool}");

        let random_i8: i8 = rand::random();
        println!("random_i8: {random_i8}");

        let random_f32: f32 = rand::random();
        println!("random_f32: {random_f32}");
    }

    {
        use rand::Rng;
        let random_number = rand::thread_rng().gen_range(1..=100);
        println!("random_number: {random_number}");
    }
}
cargo run
random_bool: true
random_i8: 69
random_f32: 0.59957224
random_number: 59