Created
January 27, 2018 20:04
-
-
Save 0rbadvent/70c7a2cac66fb57f9acae98f16496ac4 to your computer and use it in GitHub Desktop.
my first rust code :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate rand; | |
use rand::Rng; | |
fn main() { | |
for i in 0..20 { | |
println!("[{}.] {}", i + 1, output()); | |
} | |
} | |
fn get_lotterynumbers() -> Vec<i32> { | |
let min = 1; | |
let max = 49; | |
let mut numbers = vec![]; | |
loop { | |
let num = random_number(min, max); | |
if numbers.contains(&num) { | |
continue; | |
} else { | |
numbers.push(num); | |
} | |
if numbers.len() == 6 { | |
break; | |
} | |
} | |
numbers.sort(); | |
return numbers; | |
} | |
fn get_ticketnumbers() -> Vec<i32> { | |
let min = 0; | |
let max = 9; | |
let mut numbers = vec![]; | |
loop { | |
numbers.push(random_number(min, max)); | |
if numbers.len() == 7 { | |
break; | |
} | |
} | |
return numbers; | |
} | |
fn random_number(min: i32, max: i32) -> i32 { | |
return rand::thread_rng().gen_range(min, max); | |
} | |
fn output() -> String { | |
let lotterynumbers = get_lotterynumbers(); | |
let mut out_string = String::new(); | |
out_string.push_str("\n"); | |
out_string.push_str("lottery numbers: "); | |
for i in lotterynumbers.iter() { | |
out_string.push_str(&format!("| {} ", i)); | |
} | |
let ticketnumbers = get_ticketnumbers(); | |
out_string.push_str(&format!("\nticketnumber: ( ")); | |
for i in ticketnumbers.iter() { | |
out_string.push_str(&format!("{} ", i)); | |
} | |
out_string.push_str(")\n"); | |
return out_string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment