rand
Random integer sampling, string generation, selection, and shuffling.
Functions
int end
Samples a uniformly distributed integer from the half-open range [0, end).
Parameters:
| Name | Type | Description |
|---|---|---|
end |
int |
exclusive upper bound |
Returns: int - Sampled integer
Errors:
- Raises a type error if
endis not an integer - Raises an error if
end <= 0
int end start
Samples a uniformly distributed integer from the half-open range [start, end).
Parameters:
| Name | Type | Description |
|---|---|---|
end |
int |
exclusive upper bound |
start |
int |
lower bound |
Returns: int - Sampled integer
Errors:
- Raises a type error if
startorendis not an integer - Raises an error if
end <= start
string len :alphabet?
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 |
Returns: str - Randomly generated text
Errors:
- Raises a type error if
lenis not an integer oralphabetis not a string - Raises an error if
lenis negative - Raises an error if
alphabetis empty
alphabet is interpreted as characters, not raw UTF-8 bytes.
If alphabet: is omitted, it defaults to the URL-safe NanoID alphabet:
_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.
let token = string 12
assert_eq $token.len 12
let text = string 4 alphabet: "åß"
assert_eq $text.len 8
pick collection
Selects one random element from an array.
Parameters:
| Name | Type | Description |
|---|---|---|
collection |
array |
source array |
Returns: One element from collection
Errors:
- Raises a type error if
collectionis not an array - Raises an error if
collectionis empty
shuffle array
Shuffles an array in place using a uniform Fisher-Yates pass.
Parameters:
| Name | Type | Description |
|---|---|---|
array |
array |
array to mutate |
Returns: nil
Errors:
- Raises a type error if
arrayis not an array