From a570ab9f8734584a1b1795484a80b019da1c88a5 Mon Sep 17 00:00:00 2001 From: Jakub Antolak Date: Mon, 6 Jan 2025 01:23:41 +0100 Subject: [PATCH 1/2] Add rng_gaussian method to the whiskers' context --- crates/whiskers/src/context.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/whiskers/src/context.rs b/crates/whiskers/src/context.rs index 562d36d..6a992b8 100644 --- a/crates/whiskers/src/context.rs +++ b/crates/whiskers/src/context.rs @@ -1,6 +1,6 @@ use rand::distributions::uniform::SampleUniform; use rand::Rng; -use rand_distr::{Distribution, WeightedAliasIndex}; +use rand_distr::{Distribution, Normal, WeightedAliasIndex}; use std::{fmt::Debug, ops::Range}; use vsvg::Point; @@ -100,6 +100,13 @@ impl<'a> Context<'a> { Point::new(x, y) } + /// Helper function to return a number with a Gaussian (normal) distribution + pub fn rng_gaussian(&mut self, mean: f64, std_dev: f64) -> f64 { + let normal = Normal::new(mean, std_dev).expect("Failed to create normal distribution"); + + normal.sample(&mut self.rng) + } + /// Helper function to display an inspect parameter in the inspect variables UI pub fn inspect(&mut self, key: impl AsRef, value: impl Debug) { self.inspect_variables From c341d286a98e046cd6f9a02edb58fe7fb859e4a9 Mon Sep 17 00:00:00 2001 From: Jakub Antolak Date: Mon, 6 Jan 2025 01:28:21 +0100 Subject: [PATCH 2/2] add panic docs --- crates/whiskers/src/context.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/whiskers/src/context.rs b/crates/whiskers/src/context.rs index 6a992b8..e86101b 100644 --- a/crates/whiskers/src/context.rs +++ b/crates/whiskers/src/context.rs @@ -101,6 +101,10 @@ impl<'a> Context<'a> { } /// Helper function to return a number with a Gaussian (normal) distribution + /// + /// # Panics + /// + /// Panics when the `rand_distr` can't create a Normal distribution struct instance. pub fn rng_gaussian(&mut self, mean: f64, std_dev: f64) -> f64 { let normal = Normal::new(mean, std_dev).expect("Failed to create normal distribution");