random

thefittest.utils.random.cauchy_distribution(loc: float64, scale: float64, size: int64) ndarray[Any, dtype[float64]]

Generate an array of random numbers from a Cauchy distribution.

Parameters:
locnp.float64

The location parameter of the Cauchy distribution.

scalenp.float64

The scale parameter of the Cauchy distribution.

sizenp.int64

The size of the array to generate.

Returns:
NDArray[np.float64]

An array of random numbers drawn from a Cauchy distribution.

Examples

>>> import numpy as np
>>> from thefittest.utils.random import cauchy_distribution
>>>
>>> # Generate an array of 10 random numbers from a Cauchy distribution
>>> loc_value = 0.0
>>> scale_value = 1.0
>>> size_value = 10
>>> cauchy_result = cauchy_distribution(loc_value, scale_value, size_value)
>>>
>>> print("Cauchy Distribution Result:", cauchy_result)
Cauchy Distribution Result: ...
thefittest.utils.random.check_random_state(seed: int | RandomState | None = None)

Turn seed into a np.random.RandomState instance

Parameters:
seedNone | int | instance of RandomState

If seed is None, return the RandomState singleton used by np.random. If seed is an int, return a new RandomState instance seeded with seed. If seed is already a RandomState instance, return it. Otherwise raise ValueError.

thefittest.utils.random.flip_coin(threshold)

Simulate a biased coin flip.

Parameters:
thresholdfloat64

The threshold for the biased coin flip. Should be in the range [0, 1].

Returns:
boolean

Returns True with a probability equal to the given threshold and False with a complementary probability.

Notes

The function simulates a biased coin flip with a specified threshold. If the threshold is 0.5, it behaves like a fair coin flip. A threshold greater than 0.5 biases the result towards True, while a threshold less than 0.5 biases the result towards False.

Examples

>>> from thefittest.utils.random import flip_coin
>>>
>>> # Example of a biased coin flip with a threshold of 0.5
>>> result = flip_coin(0.5)
>>> print("Coin Flip Result:", result)
Coin Flip Result: ...
thefittest.utils.random.generator1() float
thefittest.utils.random.generator2() int
thefittest.utils.random.numba_seed(seed_value)
thefittest.utils.random.randint(low, high, size)

Generate an array of random integers from a discrete uniform distribution.

Parameters:
lowint

The lowest integer to be drawn from the distribution.

highint

The highest integer to be drawn from the distribution.

sizeint

The number of integers to generate.

Returns:
NDArray[int64]

An array of random integers.

Notes

The generated integers follow a discrete uniform distribution.

Examples

>>> from numba import jit
>>> import numpy as np
>>> from thefittest.utils.random import randint
>>> # Example of generating random integers
>>> result = randint(low=1, high=10, size=5)
>>> print("Random Integers:", result)
Random Integers: ...
thefittest.utils.random.random_sample(range_size: int64, quantity: int64, replace: bool = True) ndarray[Any, dtype[int64]]

Generate a random sample from a range.

Parameters:
range_sizenp.int64

The size of the range to sample from.

quantitynp.int64

The number of elements to sample.

replacebool

Whether sampling is done with replacement. Default is True.

Returns:
NDArray[np.int64]

An array of sampled indices.

Examples

>>> from thefittest.utils.random import random_sample
>>>
>>> # Example with replacement
>>> sampled_indices = random_sample(range_size=10, quantity=3, replace=True)
>>> print("Sampled Indices:", sampled_indices)
Sampled Indices: ...
>>>
>>> # Example without replacement
>>> sampled_indices_no_replace = random_sample(range_size=10, quantity=3, replace=False)
>>> print("Sampled Indices (No Replace):", sampled_indices_no_replace)
Sampled Indices (No Replace): ...
thefittest.utils.random.random_weighted_sample(weights: ndarray[Any, dtype[float64]], quantity: int64 | int = 1, replace: bool = True) ndarray[Any, dtype[int64]]

Generate a random weighted sample.

Parameters:
weightsNDArray[np.float64]

1D array of weights representing the probability of each element being selected.

quantityUnion[np.int64, int]

The number of elements to sample. Default is 1.

replacebool

Whether sampling is done with replacement. Default is True.

Returns:
NDArray[np.int64]

An array of sampled indices.

Examples

>>> from thefittest.utils.random import random_weighted_sample
>>> import numpy as np
>>>
>>> # Example with replacement
>>> weights = np.array([0.3, 0.2, 0.5])
>>> sampled_indices = random_weighted_sample(weights, quantity=2, replace=True)
>>> print("Sampled Indices:", sampled_indices)
Sampled Indices: ...
>>>
>>> # Example without replacement
>>> sampled_indices_no_replace = random_weighted_sample(weights, quantity=2, replace=False)
>>> print("Sampled Indices (No Replace):", sampled_indices_no_replace)
Sampled Indices (No Replace): ...
thefittest.utils.random.sattolo_shuffle(arr)

Perform Sattolo’s algorithm for in-place array shuffling.

Parameters:
arrint64[:]

Input array to be shuffled.

Returns:
int64[:]

Shuffled array.

Notes

Sattolo’s algorithm generates a random cyclic permutation of a given array.

Examples

>>> from numba import jit
>>> import numpy as np
>>> from thefittest.utils.random import sattolo_shuffle
>>> # Example of using Sattolo's shuffle
>>> arr = np.array([1, 2, 3, 4, 5], dtype = np.int64)
>>> print("Shuffled Array:", sattolo_shuffle(arr))
Shuffled Array: ...
thefittest.utils.random.sattolo_shuffle_2d(arr)

Perform Sattolo’s algorithm for in-place shuffling of rows in a 2D array.

Parameters:
arrint64[:, :]

Input 2D array to be shuffled.

Returns:
int64[:, :]

Shuffled 2D array.

Notes

Sattolo’s algorithm generates a random cyclic permutation of rows in a 2D array.

Examples

>>> from numba import jit
>>> import numpy as np
>>> from thefittest.utils.random import sattolo_shuffle_2d
>>> # Example of using Sattolo's shuffle for a 2D array
>>> arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> print("Shuffled 2D Array:", sattolo_shuffle_2d(arr_2d))
Shuffled 2D Array: ...
thefittest.utils.random.uniform(low: float64, high: float64, size: int64)

Generate an array of random samples from a uniform distribution.

Parameters:
lownp.float64

The lower boundary of the output interval. All values generated will be greater than or equal to low.

highnp.float64

The upper boundary of the output interval. All values generated will be less or equal to high.

sizenp.int64

The number of elements to generate.

Returns:
NDArray[np.float64]

An 1D array of random samples drawn from the uniform distribution.

Notes

The generated samples follow a uniform distribution, where each value within the specified range has an equal probability of being selected.

Examples

>>> from thefittest.utils.random import uniform
>>>
>>> # Example of generating random samples
>>> result = uniform(low=0.0, high=1.0, size=5)
>>> print("Random Samples:", result)
Random Samples: ...