summaryrefslogtreecommitdiff
path: root/2026/games_submissions/asrael_io/source/src/rng.rs
diff options
context:
space:
mode:
authorLLLL Colonq <llll@colonq>2026-07-31 05:00:49 -0400
committerLLLL Colonq <llll@colonq>2026-07-31 05:00:49 -0400
commitea5332e4a1a035e888709bee278fcafb182f7922 (patch)
tree7e1520391e7f04fa3df425fd9ed02008f748cdfd /2026/games_submissions/asrael_io/source/src/rng.rs
parent3ec03ba57474aa4320cb7901980b55706adbc111 (diff)
Update
Diffstat (limited to '2026/games_submissions/asrael_io/source/src/rng.rs')
-rw-r--r--2026/games_submissions/asrael_io/source/src/rng.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/2026/games_submissions/asrael_io/source/src/rng.rs b/2026/games_submissions/asrael_io/source/src/rng.rs
new file mode 100644
index 0000000..6e46de0
--- /dev/null
+++ b/2026/games_submissions/asrael_io/source/src/rng.rs
@@ -0,0 +1,35 @@
+pub struct Rng(u32);
+
+impl Rng {
+ pub fn new(seed: u32) -> Self {
+ let mut rng = Self(seed.max(1));
+ rng.next();
+ rng
+ }
+
+ pub fn next(&mut self) -> u32 {
+ self.0 ^= self.0 << 13;
+ self.0 ^= self.0 >> 17;
+ self.0 ^= self.0 << 5;
+
+ self.0
+ }
+
+ pub fn range(&mut self, n: u32) -> u32 {
+ self.next() % n
+ }
+
+ pub fn f32(&mut self) -> f32 {
+ self.next() as f32 / u32::MAX as f32
+ }
+
+ pub fn chance(&mut self, p: f32) -> bool {
+ self.f32() < p
+ }
+}
+
+impl Default for Rng {
+ fn default() -> Self {
+ Self::new(u32::from_be_bytes(*b"CLNK"))
+ }
+}