Skip to content

Commit ca6f6ce

Browse files
committed
adjust random methods
1 parent 7e655ec commit ca6f6ce

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

py5_docs/Reference/api_en/Sketch_random_choice.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ category = math
55
subcategory = random
66

77
@@ signatures
8-
random_choice(objects: list[Any]) -> Any
8+
random_choice(seq: Sequence[Any]) -> Any
99

1010
@@ variables
11-
objects: list[Any] - list of objects to choose from
11+
seq: Sequence[Any] - list of objects to choose from
1212

1313
@@ description
1414
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.

py5_docs/Reference/api_en/Sketch_random_permutation.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ subcategory = random
88
random_permutation(seq: Sequence[Any]) -> Sequence[Any]
99

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

1313
@@ description
1414
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.

py5_docs/Reference/api_en/Sketch_random_sample.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ category = math
55
subcategory = random
66

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

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

1515
@@ description

py5_resources/py5_module/py5/mixins/math.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import types
2424
import warnings
2525
from pathlib import Path
26-
from typing import Any, Union, overload, Sequence
26+
from typing import Any, Sequence, Union, overload
2727

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

331-
def random_choice(self, objects: list[Any]) -> Any:
331+
def random_choice(self, seq: Sequence[Any]) -> Any:
332332
"""$class_Sketch_random_choice"""
333-
if len(objects):
334-
return objects[self._rng.integers(0, len(objects))]
333+
if len(seq):
334+
return seq[self._rng.integers(0, len(seq))]
335335
else:
336336
return None
337337

338338
def random_sample(
339-
self, objects: list[Any], size: int = 1, replace: bool = True
340-
) -> list[Any]:
339+
self, seq: Sequence[Any], size: int = 1, replace: bool = True
340+
) -> Sequence[Any]:
341341
"""$class_Sketch_random_sample"""
342-
if len(objects):
343-
if isinstance(objects, types.GeneratorType):
344-
objects = list(objects)
345-
indices = self._rng.choice(range(len(objects)), size=size, replace=replace)
346-
if not isinstance(objects, list):
342+
if len(seq):
343+
if isinstance(seq, types.GeneratorType):
344+
seq = list(seq)
345+
indices = self._rng.choice(range(len(seq)), size=size, replace=replace)
346+
if not isinstance(seq, list):
347347
try:
348-
return objects[indices]
348+
return seq[indices]
349349
except:
350350
pass
351-
return [objects[idx] for idx in indices]
351+
return [seq[idx] for idx in indices]
352352
else:
353353
return []
354354

0 commit comments

Comments
 (0)