Skip to content

Commit 7f101c2

Browse files
authored
Add Context::rng_gaussian() (#164)
1 parent d3cebbf commit 7f101c2

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

crates/whiskers/src/context.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rand::distributions::uniform::SampleUniform;
22
use rand::Rng;
3-
use rand_distr::{Distribution, WeightedAliasIndex};
3+
use rand_distr::{Distribution, Normal, WeightedAliasIndex};
44
use std::{fmt::Debug, ops::Range};
55
use vsvg::Point;
66

@@ -100,6 +100,17 @@ impl<'a> Context<'a> {
100100
Point::new(x, y)
101101
}
102102

103+
/// Helper function to return a number with a Gaussian (normal) distribution
104+
///
105+
/// # Panics
106+
///
107+
/// Panics when the `rand_distr` can't create a Normal distribution struct instance.
108+
pub fn rng_gaussian(&mut self, mean: f64, std_dev: f64) -> f64 {
109+
let normal = Normal::new(mean, std_dev).expect("Failed to create normal distribution");
110+
111+
normal.sample(&mut self.rng)
112+
}
113+
103114
/// Helper function to display an inspect parameter in the inspect variables UI
104115
pub fn inspect(&mut self, key: impl AsRef<str>, value: impl Debug) {
105116
self.inspect_variables

0 commit comments

Comments
 (0)