random_functions¶
shortfx.fxNumeric.random_functions
¶
Random number generation module.
Provides pure-function wrappers for random number generation, suitable for
MCP/LLM tool exposure. All functions use the module-level random generator
which can be seeded via :func:set_random_seed for reproducibility.
Functions¶
random_array(rows: int = 1, columns: int = 1, min_value: float = 0.0, max_value: float = 1.0, whole_number: bool = False) -> List[List[float]]
¶
Returns an array of random numbers.
Description
Generates a 2-D array (list of lists) filled with random numbers. Can return either floats in [min, max) or integers in [min, max]. Equivalent to Excel RANDARRAY.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
int
|
Number of rows (default 1). |
1
|
columns
|
int
|
Number of columns (default 1). |
1
|
min_value
|
float
|
Minimum value (default 0.0). |
0.0
|
max_value
|
float
|
Maximum value (default 1.0). |
1.0
|
whole_number
|
bool
|
If True, return integers; if False, return floats. |
False
|
Returns:
| Type | Description |
|---|---|
List[List[float]]
|
A list of lists of random numbers. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If rows or columns < 1, or min_value > max_value. |
Example
set_random_seed(42) random_array(2, 3, 1, 10, True) [[2, 1, 10], [4, 10, 4]]
Complexity: O(rows * columns)
Source code in shortfx/fxNumeric/random_functions.py
random_bool(probability: float = 0.5) -> bool
¶
Return a random boolean with the given probability of True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
probability
|
float
|
Probability of returning True, in [0, 1]. Defaults to 0.5. |
0.5
|
Returns:
| Type | Description |
|---|---|
bool
|
True with the given probability, False otherwise. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If probability is not in [0, 1]. |
Example
set_random_seed(42) random_bool(0.9) True
Complexity: O(1)
Source code in shortfx/fxNumeric/random_functions.py
random_choice(items: Sequence[Any]) -> Any
¶
Return a random element from a non-empty sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[Any]
|
Non-empty sequence to choose from. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
A randomly selected element. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If items is empty. |
Example
set_random_seed(42) random_choice(['a', 'b', 'c', 'd']) 'a'
Complexity: O(1)
Source code in shortfx/fxNumeric/random_functions.py
random_float(low: float = 0.0, high: float = 1.0) -> float
¶
Return a random float in the range [low, high).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
low
|
float
|
Lower bound (inclusive). Defaults to 0.0. |
0.0
|
high
|
float
|
Upper bound (exclusive). Defaults to 1.0. |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
Random float between low (inclusive) and high (exclusive). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If low >= high. |
Example
set_random_seed(42) round(random_float(0, 100), 2) 63.94
Complexity: O(1)
Source code in shortfx/fxNumeric/random_functions.py
random_gaussian(mean: float = 0.0, std: float = 1.0) -> float
¶
Return a random float from a Gaussian (normal) distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mean
|
float
|
Mean of the distribution. Defaults to 0.0. |
0.0
|
std
|
float
|
Standard deviation. Defaults to 1.0. |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
Random float drawn from N(mean, std^2). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If std < 0. |
Example
set_random_seed(42) round(random_gaussian(0, 1), 4) -0.4008
Complexity: O(1)
Source code in shortfx/fxNumeric/random_functions.py
random_int(low: int, high: int) -> int
¶
Return a random integer in the inclusive range [low, high].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
low
|
int
|
Lower bound (inclusive). |
required |
high
|
int
|
Upper bound (inclusive). |
required |
Returns:
| Type | Description |
|---|---|
int
|
Random integer between low and high. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If low > high. |
Example
set_random_seed(42) random_int(1, 10) 2
Complexity: O(1)
Source code in shortfx/fxNumeric/random_functions.py
random_sample(items: Sequence[Any], k: int) -> List[Any]
¶
Return k unique elements chosen from the sequence without replacement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[Any]
|
Sequence to sample from. |
required |
k
|
int
|
Number of elements to sample. |
required |
Returns:
| Type | Description |
|---|---|
List[Any]
|
List of k unique randomly selected elements. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If k > len(items) or k < 0. |
Example
set_random_seed(42) random_sample([1, 2, 3, 4, 5], 3) [4, 1, 5]
Complexity: O(k)
Source code in shortfx/fxNumeric/random_functions.py
random_shuffle(items: Sequence[Any]) -> List[Any]
¶
Return a new list with the elements randomly shuffled.
The original sequence is not modified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[Any]
|
Sequence to shuffle. |
required |
Returns:
| Type | Description |
|---|---|
List[Any]
|
New list with elements in random order. |
Example
set_random_seed(42) random_shuffle([1, 2, 3, 4, 5]) [4, 2, 5, 1, 3]
Complexity: O(n)
Source code in shortfx/fxNumeric/random_functions.py
random_uuid() -> str
¶
Generate a random UUID version 4 as a string.
Returns:
| Type | Description |
|---|---|
str
|
UUID v4 string (e.g. |
Example
isinstance(random_uuid(), str) True
Complexity: O(1)
Source code in shortfx/fxNumeric/random_functions.py
random_weighted_choice(items: Sequence[Any], weights: Sequence[float]) -> Any
¶
Return a random element using weighted probabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Sequence[Any]
|
Non-empty sequence of items. |
required |
weights
|
Sequence[float]
|
Corresponding positive weights (same length as items). |
required |
Returns:
| Type | Description |
|---|---|
Any
|
A single randomly selected element. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If items is empty or lengths mismatch. |
Example
set_random_seed(42) random_weighted_choice(['a', 'b', 'c'], [1, 1, 98]) 'c'
Complexity: O(n)
Source code in shortfx/fxNumeric/random_functions.py
set_random_seed(seed: int) -> None
¶
Set the random seed for reproducible generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int
|
Integer seed value. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None. |
Example
set_random_seed(42)
Complexity: O(1)