Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Context::rng_gaussian() #164

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion crates/whiskers/src/context.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -100,6 +100,17 @@ impl<'a> Context<'a> {
Point::new(x, y)
}

/// 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");

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<str>, value: impl Debug) {
self.inspect_variables
Expand Down
Loading