From 2ae2b97ea1f69f9c0f97e40ff62be59867cbe8cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?charlotte=20=F0=9F=8C=B8?= Date: Tue, 1 Dec 2026 14:16:21 -0800 Subject: [PATCH] Random and noise additions. --- crates/processing_pyo3/processing_py/math.py | 12 ++++++++---- crates/processing_pyo3/src/lib.rs | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/crates/processing_pyo3/processing_py/math.py b/crates/processing_pyo3/processing_py/math.py index de9bbf8a..20f588f7 100644 --- a/crates/processing_pyo3/processing_py/math.py +++ b/crates/processing_pyo3/processing_py/math.py @@ -134,15 +134,19 @@ def dist(*args): raise TypeError(f"dist() takes 4 or 6 arguments ({len(args)} given)") -def random(low, high=None): - """`random(high)` is in [0, high), `random(low, high)` in [low, high).""" +def random(low=None, high=None): + """`random()` is in [0, 1), `random(high)` in [0, high), `random(low, high)` in [low, high); `random(items)` picks one.""" + if low is None: + return _random.random() if high is None: + if hasattr(low, "__getitem__"): + return _random.choice(low) low, high = 0.0, low return low + _random.random() * (high - low) -def random_gaussian(): - return _random.gauss(0.0, 1.0) +def random_gaussian(mean=0.0, sd=1.0): + return _random.gauss(mean, sd) def random_seed(seed): diff --git a/crates/processing_pyo3/src/lib.rs b/crates/processing_pyo3/src/lib.rs index feac7b7c..56497c2b 100644 --- a/crates/processing_pyo3/src/lib.rs +++ b/crates/processing_pyo3/src/lib.rs @@ -1034,9 +1034,23 @@ pub mod processing_py { NOISE.with(|n| n.borrow_mut().detail(octaves, falloff)); } + /// Any number; equal whole numbers seed alike (`5` and `5.0`). #[pyfunction] - fn noise_seed(seed: u64) { + fn noise_seed(seed: &Bound<'_, PyAny>) -> PyResult<()> { + let seed = if let Ok(seed) = seed.extract::() { + seed as u64 + } else if let Ok(seed) = seed.extract::() { + seed + } else { + let seed = seed.extract::()?; + if seed.fract() == 0.0 && seed.abs() < i64::MAX as f64 { + seed as i64 as u64 + } else { + seed.to_bits() + } + }; NOISE.with(|n| n.borrow_mut().seed(seed)); + Ok(()) } #[pyfunction]