From 88ed908faad5ca5314e6de4bd3f00fcbd8929422 Mon Sep 17 00:00:00 2001
From: Mark <mark@betalupi.com>
Date: Sun, 2 Mar 2025 10:38:06 -0800
Subject: [PATCH] Add noise to rng

---
 README.md         |  1 -
 tetros/src/lib.rs | 12 +++++++++++-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index 0c93550..4df526e 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,4 @@
 - quick drop
-- better rng
 - fix asm loader
 - document everything
 - prettier pictures
\ No newline at end of file
diff --git a/tetros/src/lib.rs b/tetros/src/lib.rs
index 5e48eb9..7eb0c7d 100644
--- a/tetros/src/lib.rs
+++ b/tetros/src/lib.rs
@@ -6,7 +6,7 @@
 
 use core::arch::asm;
 use lazy_static::lazy_static;
-use rand::{rngs::SmallRng, SeedableRng};
+use rand::{rngs::SmallRng, Rng, SeedableRng};
 use spin::Mutex;
 
 use drivers::{pic::PICDriver, vga::Vga13h};
@@ -89,6 +89,16 @@ enum InputKey {
 }
 
 extern "x86-interrupt" fn keyboard_handler(_stack_frame: InterruptStackFrame) {
+	{
+		// Re-seed our rng using user input.
+		// This is a simple hack that makes our
+		// "random" tile selector less deterministic.
+		let mut rng = RNG.lock();
+		let past: u64 = rng.random();
+		let tcr = u64::from(*TICK_COUNTER.lock());
+		*rng = SmallRng::seed_from_u64(past + tcr);
+	}
+
 	'key_block: {
 		let scancode = unsafe { inb(0x60) };
 		let key = match scancode {