Skip to content

Commit 2ae2b97

Browse files
committed
Random and noise additions.
1 parent 3709720 commit 2ae2b97

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

‎crates/processing_pyo3/processing_py/math.py‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,19 @@ def dist(*args):
134134
raise TypeError(f"dist() takes 4 or 6 arguments ({len(args)} given)")
135135

136136

137-
def random(low, high=None):
138-
"""`random(high)` is in [0, high), `random(low, high)` in [low, high)."""
137+
def random(low=None, high=None):
138+
"""`random()` is in [0, 1), `random(high)` in [0, high), `random(low, high)` in [low, high); `random(items)` picks one."""
139+
if low is None:
140+
return _random.random()
139141
if high is None:
142+
if hasattr(low, "__getitem__"):
143+
return _random.choice(low)
140144
low, high = 0.0, low
141145
return low + _random.random() * (high - low)
142146

143147

144-
def random_gaussian():
145-
return _random.gauss(0.0, 1.0)
148+
def random_gaussian(mean=0.0, sd=1.0):
149+
return _random.gauss(mean, sd)
146150

147151

148152
def random_seed(seed):

‎crates/processing_pyo3/src/lib.rs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,9 +1034,23 @@ pub mod processing_py {
10341034
NOISE.with(|n| n.borrow_mut().detail(octaves, falloff));
10351035
}
10361036

1037+
/// Any number; equal whole numbers seed alike (`5` and `5.0`).
10371038
#[pyfunction]
1038-
fn noise_seed(seed: u64) {
1039+
fn noise_seed(seed: &Bound<'_, PyAny>) -> PyResult<()> {
1040+
let seed = if let Ok(seed) = seed.extract::<i64>() {
1041+
seed as u64
1042+
} else if let Ok(seed) = seed.extract::<u64>() {
1043+
seed
1044+
} else {
1045+
let seed = seed.extract::<f64>()?;
1046+
if seed.fract() == 0.0 && seed.abs() < i64::MAX as f64 {
1047+
seed as i64 as u64
1048+
} else {
1049+
seed.to_bits()
1050+
}
1051+
};
10391052
NOISE.with(|n| n.borrow_mut().seed(seed));
1053+
Ok(())
10401054
}
10411055

10421056
#[pyfunction]

0 commit comments

Comments
 (0)