rand
Random integer sampling, string generation, selection, and shuffling.
Functions
int start … -> Int
Samples a uniformly distributed integer from [0, end) or [start, end).
Parameters
| Name | Type | Description |
|---|---|---|
start |
Int |
Inclusive lower bound, or the exclusive upper bound when
end is omitted. |
end? |
Int |
Exclusive upper bound. |
Errors
ValueError if end <= 0, or end <= start when both
are given.
Example
pick[T] collection -> T
Selects one random element from an array.
Parameters
| Name | Type | Description |
|---|---|---|
collection |
BaseArray[T] |
Source array. |
Errors
ValueError if collection is empty.
Example
shuffle[T] array
Shuffles an array in place using a uniform Fisher-Yates pass.
Parameters
| Name | Type | Description |
|---|---|---|
array |
Array[T] |
Array to mutate. |
Example
let items = [1, 2, 3, 4]
shuffle $items
let sorted = [...items]
sorted.sort()
assert_eq $sorted [1, 2, 3, 4]
string len … -> Str
Generates a random string by sampling characters from alphabet.
Parameters
| Name | Type | Description |
|---|---|---|
len |
Int |
Number of characters to emit. |
:alphabet? |
Str |
Characters to sample from. |
:alphabet
The alphabet is interpreted as characters, not raw UTF-8 bytes. When
omitted, it is _-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.
Errors
ValueError if len is negative or alphabet is empty.