Skip to content

Commit

Permalink
pcg
Browse files Browse the repository at this point in the history
  • Loading branch information
SymmetricChaos committed Oct 1, 2023
1 parent 58bf11a commit d5009c2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion rngs/src/lcg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Default for Lcg {
impl ClassicRng for Lcg {
fn step(&mut self) {
// No overflows can happen here because the inputs are are u32 initially
let m = (self.multiplier as u64 * self.state as u64) % self.modulus as u64;
let m = (self.state as u64 * self.multiplier as u64) % self.modulus as u64;
self.state = ((m + self.increment as u64) % self.modulus as u64) as u32;
}
}
3 changes: 2 additions & 1 deletion rngs/src/pcg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::traits::ClassicRng;

#[derive(Debug, PartialEq, Eq)]
pub enum PcgTransform {
Rs,
Rr,
Expand Down Expand Up @@ -53,7 +54,7 @@ impl Pcg {

impl ClassicRng for Pcg {
fn step(&mut self) {
self.state = (self.multiplier)
self.state = (self.state)
.wrapping_mul(self.multiplier)
.wrapping_add(self.increment);
}
Expand Down
14 changes: 7 additions & 7 deletions src/rng_panel/pcg_controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rngs::{
ClassicRng,
};

use crate::ui_elements::{filter_and_parse_u32, UiElements};
use crate::ui_elements::{filter_and_parse_u64, UiElements};

use super::ClassicRngFrame;

Expand All @@ -28,7 +28,7 @@ impl Default for PcgFrame {
}

impl PcgFrame {
fn input_control(ui: &mut egui::Ui, string: &mut String, n: &mut u32) {
fn input_control(ui: &mut egui::Ui, string: &mut String, n: &mut u64) {
if ui
.add_sized(
[40.0, 20.0],
Expand All @@ -38,7 +38,7 @@ impl PcgFrame {
)
.changed()
{
filter_and_parse_u32(n, string);
filter_and_parse_u64(n, string);
}
}

Expand All @@ -52,6 +52,9 @@ impl PcgFrame {
impl ClassicRngFrame for PcgFrame {
fn ui(&mut self, ui: &mut egui::Ui, _errors: &mut String) {
ui.subheading("Calculation");
let m = (self.rng.state)
.wrapping_mul(self.rng.multiplier)
.wrapping_add(self.rng.increment);
ui.horizontal(|ui| {
ui.subheading("(");
Self::input_control(ui, &mut self.state_string, &mut self.rng.state);
Expand All @@ -60,9 +63,7 @@ impl ClassicRngFrame for PcgFrame {
ui.subheading(" + ");
Self::input_control(ui, &mut self.increment_string, &mut self.rng.increment);
ui.subheading(" = ");
let mut m =
(self.rng.multiplier as u64 * self.rng.state as u64) % self.rng.modulus as u64;
m = (m + self.rng.increment as u64) % self.rng.modulus as u64;

ui.false_control_string(format!("{m}"));
});

Expand Down Expand Up @@ -114,7 +115,6 @@ impl ClassicRngFrame for PcgFrame {
self.rng.state = rng.gen();
self.rng.multiplier = rng.gen();
self.rng.increment = rng.gen();
self.rng.modulus = rng.gen();
self.set_all_strings();
}

Expand Down

0 comments on commit d5009c2

Please sign in to comment.