-
-
Save rust-play/20dd09cda3cdc8014f96b1be11066051 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or 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
//! this script recreates the book | |
//! "It's not a sin: An Introduction to the Trigonometric Values of Standard Functions" | |
extern crate rand; // 0.8.4; | |
use rand::prelude::*; | |
const DEGREES_RANGE: std::ops::RangeInclusive<i16> = -2000..=2000; | |
const NUMBER_QUESTIONS: i16 = 20; // questions per function. 250 in the book | |
fn main() { | |
let mut rng = thread_rng(); | |
let functions: Vec<(&str, fn(f64) -> f64)> = | |
vec![("sin", f64::sin), ("cos", f64::cos), ("tan", f64::tan)]; | |
for f in functions { | |
println!("{:?} questions\n{}\n", f.0, "-".repeat(80)); | |
for q in 1..=NUMBER_QUESTIONS { | |
let deg = rng.gen_range(DEGREES_RANGE); | |
println!( | |
"Question {}. Which of these is closest to the value of {}({}º)?", | |
q, f.0, deg | |
); | |
let right_answer = f.1((deg as f64).to_radians()); | |
let mut a = vec![format!("{:.2}", right_answer)]; | |
for _ in 0..5 { | |
a.push(format![ | |
"{:.2}", | |
f.1((rng.gen_range(DEGREES_RANGE) as f64).to_radians()) | |
]); | |
} | |
a.shuffle(&mut rng); | |
println!( | |
"A. {}\nB. {}\nC. {}\nD. {}\nE. {}\nF. {}", | |
a[0], a[1], a[2], a[3], a[4], a[5] | |
); | |
println!( | |
"Answer {}. {}({}º) is approximately equal to {:.2}\n", | |
q, f.0, deg, right_answer | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment