-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
/
Copy path_random.py
38 lines (26 loc) · 905 Bytes
/
_random.py
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
import string
import numpy as np
from pandas._typing import NpDtype
def randbool(size=(), p: float = 0.5):
return np.random.rand(*size) <= p
RANDS_CHARS = np.array(list(string.ascii_letters + string.digits), dtype=(np.str_, 1))
RANDU_CHARS = np.array(
list("".join(map(chr, range(1488, 1488 + 26))) + string.digits),
dtype=(np.unicode_, 1),
)
def rands_array(nchars, size, dtype: NpDtype = "O", replace=True) -> np.ndarray:
"""
Generate an array of byte strings.
"""
retval = (
np.random.choice(RANDS_CHARS, size=nchars * np.prod(size), replace=replace)
.view((np.str_, nchars))
.reshape(size)
)
return retval.astype(dtype)
def rands(nchars) -> str:
"""
Generate one random byte string.
See `rands_array` if you want to create an array of random strings.
"""
return "".join(np.random.choice(RANDS_CHARS, nchars))