_random.py 710 B

1234567891011121314151617181920212223242526272829
  1. import string
  2. import numpy as np
  3. from pandas._typing import NpDtype
  4. RANDS_CHARS = np.array(list(string.ascii_letters + string.digits), dtype=(np.str_, 1))
  5. def rands_array(nchars, size, dtype: NpDtype = "O", replace: bool = True) -> np.ndarray:
  6. """
  7. Generate an array of byte strings.
  8. """
  9. retval = (
  10. np.random.choice(RANDS_CHARS, size=nchars * np.prod(size), replace=replace)
  11. .view((np.str_, nchars))
  12. .reshape(size)
  13. )
  14. return retval.astype(dtype)
  15. def rands(nchars) -> str:
  16. """
  17. Generate one random byte string.
  18. See `rands_array` if you want to create an array of random strings.
  19. """
  20. return "".join(np.random.choice(RANDS_CHARS, nchars))