Skip to content

Commit

Permalink
implement xoroshiro128 RNG
Browse files Browse the repository at this point in the history
  • Loading branch information
kralverde committed Aug 28, 2024
1 parent 8e9ea66 commit 0a35117
Show file tree
Hide file tree
Showing 6 changed files with 567 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pumpkin-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ edition.workspace = true
serde = { version = "1.0", features = ["derive"] }
fastnbt = { git = "https://github.com/owengage/fastnbt.git" }
uuid.workspace = true
colored = "2"
colored = "2"
md5 = "0.7.0"

1 change: 1 addition & 0 deletions pumpkin-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod random;
pub mod text;
31 changes: 31 additions & 0 deletions pumpkin-core/src/random/gaussian.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use super::Random;

pub trait GaussianGenerator: Random {
fn has_next_gaussian(&self) -> bool;

fn set_has_next_gaussian(&mut self, value: bool);

fn stored_next_gaussian(&self) -> f64;

fn set_stored_next_gaussian(&mut self, value: f64);

fn calculate_gaussian(&mut self) -> f64 {
if self.has_next_gaussian() {
self.set_has_next_gaussian(false);
self.stored_next_gaussian()
} else {
loop {
let d = 2f64 * self.next_f64() - 1f64;
let e = 2f64 * self.next_f64() - 1f64;
let f = d * d + e * e;

if f < 1f64 && f != 0f64 {
let g = (-2f64 * f.ln() / f).sqrt();
self.set_stored_next_gaussian(e * g);
self.set_has_next_gaussian(true);
return d * g;
}
}
}
}
}
51 changes: 51 additions & 0 deletions pumpkin-core/src/random/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
pub mod xoroshiro128;

mod gaussian;

pub trait Random {
fn split(&mut self) -> Self;

fn next_splitter(&mut self) -> impl RandomSplitter;

fn set_seed(&mut self, seed: i64);

fn next_i32(&mut self) -> i32;

fn next_bounded_i32(&mut self, bound: i32) -> i32;

fn next_inbetween_i32(&mut self, min: i32, max: i32) -> i32 {
self.next_bounded_i32(max - min + 1) + min
}

fn next_i64(&mut self) -> i64;

fn next_bool(&mut self) -> bool;

fn next_f32(&mut self) -> f32;

fn next_f64(&mut self) -> f64;

fn next_gaussian(&mut self) -> f64;

fn next_triangular(&mut self, mode: f64, deviation: f64) -> f64 {
mode + deviation * (self.next_f64() - self.next_f64())
}

fn skip(&mut self, count: i32) {
for _ in 0..count {
self.next_i32();
}
}

fn next_inbetween_i32_exclusive(&mut self, min: i32, max: i32) -> i32 {
min + self.next_bounded_i32(max - min)
}
}

pub trait RandomSplitter {
fn split_string(&self, seed: &str) -> impl Random;

fn split_i64(&self, seed: i64) -> impl Random;

fn split_pos(&self, x: i32, y: i32, z: i32) -> impl Random;
}
Loading

0 comments on commit 0a35117

Please sign in to comment.