Skip to content
Merged
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
4 changes: 2 additions & 2 deletions py5_docs/Reference/api_en/Sketch_random_choice.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ category = math
subcategory = random

@@ signatures
random_choice(objects: list[Any]) -> Any
random_choice(seq: Sequence[Any]) -> Any

@@ variables
objects: list[Any] - list of objects to choose from
seq: Sequence[Any] - list of objects to choose from

@@ description
Select a random item from a list. The list items can be of any type. If the list of objects is empty, `None` will be returned.
Expand Down
2 changes: 1 addition & 1 deletion py5_docs/Reference/api_en/Sketch_random_permutation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ subcategory = random
random_permutation(seq: Sequence[Any]) -> Sequence[Any]

@@ variables
seq: Sequence[Any] - sequence of objects for which random permutation is required.
seq: Sequence[Any] - sequence of objects for which random permutation is required

@@ description
Generates a random permutation for the given sequence. Each time the `random_permutation()` method is called, it generates and return a random permuted sequence of the given sequence.
Expand Down
4 changes: 2 additions & 2 deletions py5_docs/Reference/api_en/Sketch_random_sample.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ category = math
subcategory = random

@@ signatures
random_sample(objects: list[Any], size: int = 1, replace: bool = True) -> list[Any]
random_sample(seq: Sequence[Any], size: int = 1, replace: bool = True) -> Sequence[Any]

@@ variables
objects: list[Any] - list of objects to choose from
replace: bool = True - whether to select random items with or without replacement
seq: Sequence[Any] - list of objects to choose from
size: int = 1 - number of random items to select

@@ description
Expand Down
26 changes: 13 additions & 13 deletions py5_resources/py5_module/py5/mixins/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import types
import warnings
from pathlib import Path
from typing import Any, Union, overload, Sequence
from typing import Any, Sequence, Union, overload

import numpy as np
import numpy.typing as npt
Expand Down Expand Up @@ -328,27 +328,27 @@ def random_int(self, *args: int) -> int:
types = ",".join([type(a).__name__ for a in args])
raise TypeError(f"No matching overloads found for Sketch.random_int({types})")

def random_choice(self, objects: list[Any]) -> Any:
def random_choice(self, seq: Sequence[Any]) -> Any:
"""$class_Sketch_random_choice"""
if len(objects):
return objects[self._rng.integers(0, len(objects))]
if len(seq):
return seq[self._rng.integers(0, len(seq))]
else:
return None

def random_sample(
self, objects: list[Any], size: int = 1, replace: bool = True
) -> list[Any]:
self, seq: Sequence[Any], size: int = 1, replace: bool = True
) -> Sequence[Any]:
"""$class_Sketch_random_sample"""
if len(objects):
if isinstance(objects, types.GeneratorType):
objects = list(objects)
indices = self._rng.choice(range(len(objects)), size=size, replace=replace)
if not isinstance(objects, list):
if len(seq):
if isinstance(seq, types.GeneratorType):
seq = list(seq)
indices = self._rng.choice(range(len(seq)), size=size, replace=replace)
if not isinstance(seq, list):
try:
return objects[indices]
return seq[indices]
except:
pass
return [objects[idx] for idx in indices]
return [seq[idx] for idx in indices]
else:
return []

Expand Down