diff options
| author | Rose Hogenson <rosehogenson@posteo.net> | 2023-07-21 21:41:37 -0700 |
|---|---|---|
| committer | Rose Hogenson <rosehogenson@posteo.net> | 2023-07-21 21:41:37 -0700 |
| commit | b5caf80b8f18fcafca3ff697f219070825620695 (patch) | |
| tree | abd313556c855a109db531d04d5dc4bdf6071fba | |
| parent | 8b51759dc9b26eb9d9e9f94807d7e85a41fbf555 (diff) | |
| download | stallman-shooter-b5caf80b8f18fcafca3ff697f219070825620695.tar.zst | |
Remove dependency on the rand crate.
I don't need a dependency to generate random numbers. I know how to
generate random numbers.
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Cargo.lock | 54 | ||||
| -rw-r--r-- | Cargo.toml | 3 | ||||
| -rw-r--r-- | src/gates.rs | 8 | ||||
| -rw-r--r-- | src/main.rs | 3 | ||||
| -rw-r--r-- | src/rand.rs | 47 |
6 files changed, 55 insertions, 61 deletions
@@ -1,3 +1,4 @@ /target/ **/*.rs.bk +**/*.swp result @@ -15,17 +15,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] -name = "getrandom" -version = "0.2.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" -dependencies = [ - "cfg-if", - "libc", - "wasi", -] - -[[package]] name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -38,42 +27,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" [[package]] -name = "ppv-lite86" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" - -[[package]] -name = "rand" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" -dependencies = [ - "libc", - "rand_chacha", - "rand_core", -] - -[[package]] -name = "rand_chacha" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" -dependencies = [ - "ppv-lite86", - "rand_core", -] - -[[package]] -name = "rand_core" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" -dependencies = [ - "getrandom", -] - -[[package]] name = "sdl2" version = "0.35.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -100,7 +53,6 @@ dependencies = [ name = "stallman-shooter" version = "1.0.0" dependencies = [ - "rand", "sdl2", "sdl2-sys", ] @@ -110,9 +62,3 @@ name = "version-compare" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29" - -[[package]] -name = "wasi" -version = "0.11.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" @@ -3,9 +3,6 @@ name = "stallman-shooter" version = "1.0.0" authors = ["Rose Hogenson <rosehogenson@posteo.net>"] -[dependencies] -rand = "0.8.5" - [dependencies.sdl2] version = "0.35.2" default-features = false diff --git a/src/gates.rs b/src/gates.rs index ebc2b69..1e3630f 100644 --- a/src/gates.rs +++ b/src/gates.rs @@ -1,4 +1,5 @@ use super::constants; +use super::rand; use super::state; pub struct Gates { @@ -57,8 +58,8 @@ impl state::Positioned for Gates { fn random_position() -> (f64, f64) { ( - rand::random::<f64>() * (constants::X_DIM - 200) as f64 + 100., - rand::random::<f64>() * 100., + rand::random() * (constants::X_DIM - 200) as f64 + 100., + rand::random() * 100., ) } @@ -85,8 +86,7 @@ impl state::StraightBullet for GatesBullet { fn random_bullet<T: state::Enemy<GatesBullet>>(enemy: &T) -> GatesBullet { let multiplier = 2. + 3. * (1. - enemy.health()); - let theta: f64 = - rand::random::<f64>() * 2. * std::f64::consts::PI / 3. + std::f64::consts::PI / 6.; + let theta: f64 = rand::random() * 2. * std::f64::consts::PI / 3. + std::f64::consts::PI / 6.; let dx = f64::cos(theta) * multiplier; let dy = f64::sin(theta) * multiplier; GatesBullet::new(enemy.x(), dx, enemy.y(), dy) diff --git a/src/main.rs b/src/main.rs index 59913c0..25a4f3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod constants; mod gates; mod player; +mod rand; mod state; use state::Bullet; @@ -221,6 +222,8 @@ fn victory( } fn main() { + rand::seed().expect("failed to seed random generator"); + let sdl_context = sdl2::init().unwrap(); let video_subsystem = sdl_context.video().unwrap(); diff --git a/src/rand.rs b/src/rand.rs new file mode 100644 index 0000000..404e115 --- /dev/null +++ b/src/rand.rs @@ -0,0 +1,47 @@ +use std::fs::File; +use std::io::Read; +use std::sync::Mutex; +use std::time::SystemTime; + +static STATE: Mutex<u64> = Mutex::new(0); + +fn seed_urandom() -> Option<()> { + let mut f = File::open("/dev/urandom").ok()?; + let mut bytes = [0; 8]; + f.read_exact(&mut bytes).ok()?; + + *STATE.lock().unwrap() = u64::from_ne_bytes(bytes); + return Some(()); +} + +fn seed_time() -> Option<()> { + let n = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .ok()?; + + *STATE.lock().unwrap() = n.as_nanos() as u64; + return Some(()); +} + +pub fn seed() -> Option<()> { + match seed_urandom() { + Some(()) => return Some(()), + None => (), + } + match seed_time() { + Some(()) => return Some(()), + None => (), + } + return None; +} + +fn mix(s: &mut u64) { + // https://en.wikipedia.org/wiki/Linear_congruential_generator + *s = s.wrapping_mul(6364136223846793005).wrapping_add(1); +} + +pub fn random() -> f64 { + let mut s = STATE.lock().unwrap(); + mix(&mut s); + return (*s >> 32) as f64 / 4294967296.; +} |
