random.py 1.25 KB
Newer Older
zhangqha's avatar
zhangqha committed
1
2
3
4
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
52
53
54
55
56
57
58
59
60
61
62
63
import numpy as np


_RANDOM_GENERATOR = np.random.RandomState()


def choice(a: np.ndarray, p: np.ndarray = None):
    """Generates a random sample from a given 1-D array.

    Parameters
    ----------
    a : np.ndarray
        A random sample is generated from its elements.
    p : np.ndarray
        The probabilities associated with each entry in a.

    Returns
    -------
    np.ndarray
        arrays with results and their shapes
    """
    return _RANDOM_GENERATOR.choice(a, p=p)


def random(size=None):
    """Return random floats in the half-open interval [0.0, 1.0).

    Parameters
    ----------
    size
        Output shape.

    Returns
    -------
    np.ndarray
        Arrays with results and their shapes.
    """
    return _RANDOM_GENERATOR.random_sample(size)


def seed(val: int = None):
    """Seed the generator.

    Parameters
    ----------
    val : int
        Seed.
    """
    _RANDOM_GENERATOR.seed(val)


def shuffle(x: np.ndarray):
    """Modify a sequence in-place by shuffling its contents.

    Parameters
    ----------
    x : np.ndarray
        The array or list to be shuffled.
    """
    _RANDOM_GENERATOR.shuffle(x)


__all__ = ['choice', 'random', 'seed', 'shuffle']