Generate random numbers

    0

    0

    Giovanny Gongora

    Codiga's Rust Recipes

    numbers with different types

    use rand::Rng;
    
    fn main() {
      let mut rng = rand::thread_rng();
      let n1: u8 = rng.gen();
      let n2: u16 = rng.gen();
      println!("Random u8: {}", n1);
      println!("Random u16: {}", n2);
      println!("Random u32: {}", rng.gen::<u32>());
      println!("Random i32: {}", rng.gen::<i32>());
      println!("Random float: {}", rng.gen::<f64>());
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.