Skip to content

Commit

Permalink
Merge pull request #66 from kralverde/xoroshiro
Browse files Browse the repository at this point in the history
implement xoroshiro128 RNG
  • Loading branch information
Snowiiii authored Aug 30, 2024
2 parents f45ebb2 + fea3cf7 commit 6a45746
Show file tree
Hide file tree
Showing 6 changed files with 541 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.

3 changes: 2 additions & 1 deletion pumpkin-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ 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;
}
}
}
}
}
53 changes: 53 additions & 0 deletions pumpkin-core/src/random/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
pub mod xoroshiro128;

mod gaussian;

pub trait Random {
fn from_seed(seed: u64) -> Self;

fn split(&mut self) -> Self;

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

fn next(&mut self, bits: u64) -> u64;

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_u64(&self, seed: u64) -> impl Random;

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

0 comments on commit 6a45746

Please sign in to comment.