aboutsummaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-10-02 14:42:51 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-10-02 14:42:51 -0400
commit54e336e30f2112dc2cb0c1b85dd153667680e550 (patch)
tree6d933b3479c08e7d2f0ec00a40badcfbdb28002e /docs
parentc8c137639c99793a7a2136f2bdc8de903bf4b5ec (diff)
Update array:sample() to use optional weights and do more error checking
Diffstat (limited to 'docs')
-rw-r--r--docs/arrays.md16
1 files changed, 11 insertions, 5 deletions
diff --git a/docs/arrays.md b/docs/arrays.md
index bb5af7c1..edde43af 100644
--- a/docs/arrays.md
+++ b/docs/arrays.md
@@ -731,7 +731,7 @@ probabilities.
**Usage:**
```markdown
-sample(arr: [T], count: Int, weights: [Num]) -> [T]
+sample(arr: [T], count: Int, weights: [Num]? = ![Num]) -> [T]
```
**Parameters:**
@@ -740,10 +740,16 @@ sample(arr: [T], count: Int, weights: [Num]) -> [T]
- `count`: The number of elements to sample.
- `weights`: The probability weights for each element in the array. These
values do not need to add up to any particular number, they are relative
- weights. If no weights are provided, the default is equal probabilities.
- Negative, infinite, or NaN weights will cause a runtime error. If the number of
- weights given is less than the length of the array, elements from the rest of
- the array are considered to have zero weight.
+ weights. If no weights are given, elements will be sampled with uniform
+ probability.
+
+**Errors:**
+Errors will be raised if any of the following conditions occurs:
+- The given array has no elements and `count >= 1`
+- `count < 0` (negative count)
+- The number of weights provided doesn't match the length of the array.
+- Any weight in the weights array is negative, infinite, or `NaN`
+- The sum of the given weights is zero (zero probability for every element).
**Returns:**
A list of sampled elements from the array.