-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #259 from hx2A/fix199
random_sample() and random_choice() methods
- Loading branch information
Showing
4 changed files
with
41 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@@ meta | ||
name = random_sample() | ||
type = method | ||
category = math | ||
subcategory = random | ||
|
||
@@ signatures | ||
random_sample(objects: list[Any], size: int=1, replace: bool=True) -> npt.NDArray | ||
|
||
@@ variables | ||
objects: list[Any] - list of objects to choose from | ||
replace: bool=True - whether to select random items with or without replacement | ||
size: int=1 - number of random items to select | ||
|
||
@@ description | ||
Select random items from a list. The list items can be of any type. If multiple items are selected, this function will by default allow the same item to be selected multiple times. Set the `replace` parameter to `False` to prevent the same item from being selected multiple times. | ||
|
||
The returned value will always be a numpy array, even if only one item is sampled. If the list of objects is empty, an empty numpy array will be returned. | ||
|
||
This function's randomness can be influenced by [](sketch_random_seed), and makes calls to numpy to select the random items. | ||
|
||
@@ example | ||
def setup(): | ||
words = ["apple", "bear", "cat", "dog"] | ||
word = py5.random_sample(words) | ||
py5.println(word) # Prints one of the four words |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters