added some comments
This commit is contained in:
+28
-6
@@ -11,11 +11,25 @@ impl Rng {
|
|||||||
.duration_since(SystemTime::UNIX_EPOCH)
|
.duration_since(SystemTime::UNIX_EPOCH)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.subsec_nanos() as u64;
|
.subsec_nanos() as u64;
|
||||||
Self { state: seed ^ 0x9e3779b97f4a7c15 }
|
// Allegedly
|
||||||
|
// It's XOR'd with the seed (`subsec_nanos()`) as a **mixing step** —
|
||||||
|
// the golden ratio has a property where it distributes bits very evenly across
|
||||||
|
// the range, so even a low-entropy seed gets spread across all 64 bits
|
||||||
|
// before the first LCG iteration runs
|
||||||
|
Self {
|
||||||
|
state: seed ^ 0x9e3779b97f4a7c15,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_u64(&mut self) -> u64 {
|
fn next_u64(&mut self) -> u64 {
|
||||||
self.state = self.state.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
|
// Allegedly
|
||||||
|
// Knuth MMIX LCG constants:
|
||||||
|
// multiplier chosen for spectral quality (for uniform distribution)
|
||||||
|
// addend must be odd for full 2^64 period
|
||||||
|
self.state = self
|
||||||
|
.state
|
||||||
|
.wrapping_mul(6364136223846793005)
|
||||||
|
.wrapping_add(1442695040888963407);
|
||||||
self.state
|
self.state
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +123,7 @@ fn main() {
|
|||||||
println!("Complete {ROUNDS} questions to finish a round.\n");
|
println!("Complete {ROUNDS} questions to finish a round.\n");
|
||||||
|
|
||||||
let stdin = io::stdin();
|
let stdin = io::stdin();
|
||||||
let mut stdin = stdin.lock();
|
let mut stdin = stdin.lock(); // why lock?
|
||||||
let mut rng = Rng::new();
|
let mut rng = Rng::new();
|
||||||
let mut stats: Vec<QuestionStats> = Vec::with_capacity(ROUNDS);
|
let mut stats: Vec<QuestionStats> = Vec::with_capacity(ROUNDS);
|
||||||
|
|
||||||
@@ -156,12 +170,16 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn print_summary(stats: &[QuestionStats]) {
|
fn print_summary(stats: &[QuestionStats]) {
|
||||||
|
// voodoo
|
||||||
let total_time: Duration = stats.iter().map(|s| s.time_taken).sum();
|
let total_time: Duration = stats.iter().map(|s| s.time_taken).sum();
|
||||||
let total_mistakes: u32 = stats.iter().map(|s| s.mistakes).sum();
|
let total_mistakes: u32 = stats.iter().map(|s| s.mistakes).sum();
|
||||||
let perfect = stats.iter().filter(|s| s.mistakes == 0).count();
|
let perfect = stats.iter().filter(|s| s.mistakes == 0).count();
|
||||||
|
|
||||||
println!("=== Round Complete ===\n");
|
println!("=== Round Complete ===\n");
|
||||||
println!("{:<5} {:<12} {:<8} {:>10} {:>8}", "#", "Question", "Answer", "Time (s)", "Mistakes");
|
println!(
|
||||||
|
"{:<5} {:<12} {:<8} {:>10} {:>8}",
|
||||||
|
"#", "Question", "Answer", "Time (s)", "Mistakes"
|
||||||
|
);
|
||||||
println!("{}", "-".repeat(50));
|
println!("{}", "-".repeat(50));
|
||||||
|
|
||||||
for (i, s) in stats.iter().enumerate() {
|
for (i, s) in stats.iter().enumerate() {
|
||||||
@@ -179,7 +197,11 @@ fn print_summary(stats: &[QuestionStats]) {
|
|||||||
println!("{}", "-".repeat(50));
|
println!("{}", "-".repeat(50));
|
||||||
println!(
|
println!(
|
||||||
"{:<5} {:<12} {:<8} {:>10.2} {:>8}",
|
"{:<5} {:<12} {:<8} {:>10.2} {:>8}",
|
||||||
"Tot", "", "", total_time.as_secs_f64(), total_mistakes
|
"Tot",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
total_time.as_secs_f64(),
|
||||||
|
total_mistakes
|
||||||
);
|
);
|
||||||
println!("\nPerfect (no mistakes): {}/{}", perfect, stats.len());
|
println!("\nScore: {}/{}", perfect, stats.len());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user