Skip to content

random

Random

Bases: Designs

Super class for random designs.

Attributes:

Name Type Description
k int

The number of factors.

seed int

The seed for the random number generator.

Source code in spotpython/design/random.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Random(Designs):
    """
    Super class for random designs.

    Attributes:
        k (int): The number of factors.
        seed (int): The seed for the random number generator.
    """

    def __init__(self, k: int = 2, seed: int = 123) -> None:
        """
        Initializes a random design object.

        Args:
            k (int): The number of factors. Defaults to 2.
            seed (int): The seed for the random number generator. Defaults to 123.
        """
        super().__init__(k, seed)
        self.k = k
        self.seed = seed

    def uniform(self, n_points: int, seed: int = None) -> np.ndarray:
        """
        Generates a random design using uniform distribution.

        Args:
            n_points (int): The number of points to generate.
            seed (int, optional): The seed for the random number generator. If None, uses the instance's seed.

        Returns:
            numpy.ndarray: A 2D array of shape (n_points, k) with random values in [0, 1).

        Examples:
            >>> from spotpython.design.random import Random
            >>> random_design = Random(k=3)
            >>> random_design.uniform(n_points=5)
            array([[0.123, 0.456, 0.789],
                   [0.234, 0.567, 0.890],
                   [0.345, 0.678, 0.901],
                   [0.456, 0.789, 0.012],
                   [0.567, 0.890, 0.123]])

        """
        if seed is not None:
            seed = self.seed
        rng = np.random.default_rng(seed)
        return rng.random((n_points, self.k))

__init__(k=2, seed=123)

Initializes a random design object.

Parameters:

Name Type Description Default
k int

The number of factors. Defaults to 2.

2
seed int

The seed for the random number generator. Defaults to 123.

123
Source code in spotpython/design/random.py
14
15
16
17
18
19
20
21
22
23
24
def __init__(self, k: int = 2, seed: int = 123) -> None:
    """
    Initializes a random design object.

    Args:
        k (int): The number of factors. Defaults to 2.
        seed (int): The seed for the random number generator. Defaults to 123.
    """
    super().__init__(k, seed)
    self.k = k
    self.seed = seed

uniform(n_points, seed=None)

Generates a random design using uniform distribution.

Parameters:

Name Type Description Default
n_points int

The number of points to generate.

required
seed int

The seed for the random number generator. If None, uses the instance’s seed.

None

Returns:

Type Description
ndarray

numpy.ndarray: A 2D array of shape (n_points, k) with random values in [0, 1).

Examples:

>>> from spotpython.design.random import Random
>>> random_design = Random(k=3)
>>> random_design.uniform(n_points=5)
array([[0.123, 0.456, 0.789],
       [0.234, 0.567, 0.890],
       [0.345, 0.678, 0.901],
       [0.456, 0.789, 0.012],
       [0.567, 0.890, 0.123]])
Source code in spotpython/design/random.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def uniform(self, n_points: int, seed: int = None) -> np.ndarray:
    """
    Generates a random design using uniform distribution.

    Args:
        n_points (int): The number of points to generate.
        seed (int, optional): The seed for the random number generator. If None, uses the instance's seed.

    Returns:
        numpy.ndarray: A 2D array of shape (n_points, k) with random values in [0, 1).

    Examples:
        >>> from spotpython.design.random import Random
        >>> random_design = Random(k=3)
        >>> random_design.uniform(n_points=5)
        array([[0.123, 0.456, 0.789],
               [0.234, 0.567, 0.890],
               [0.345, 0.678, 0.901],
               [0.456, 0.789, 0.012],
               [0.567, 0.890, 0.123]])

    """
    if seed is not None:
        seed = self.seed
    rng = np.random.default_rng(seed)
    return rng.random((n_points, self.k))