Skip to content
Draft
Show file tree
Hide file tree
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
12 changes: 8 additions & 4 deletions crates/processing_pyo3/processing_py/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
16 changes: 15 additions & 1 deletion crates/processing_pyo3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i64>() {
seed as u64
} else if let Ok(seed) = seed.extract::<u64>() {
seed
} else {
let seed = seed.extract::<f64>()?;
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]
Expand Down
Loading