bases.random

Functions to generate random data.

default_options

default_options()[source]

Readonly view of the default random generation options.

Return type:

Mapping[str, Any]

get_options

get_options()[source]

Readonly view of the current random generation options.

Return type:

Mapping[str, Any]

options

options(*, seed=None, min_bytes=None, max_bytes=None, min_chars=None, max_chars=None)[source]

Returns with-statement context manager for temporary option setting:

with options(**options):
    for value in rand_data(num_samples, encoding):
        ...

See set_options for a description of the options.

Parameters:
Return type:

Iterator[None]

rand_block_chars

rand_block_chars(n=None, *, block_nchars, encoding)[source]

Generates a stream of random char blocks for a block base encoding. If a number n is given, that number of samples is yelded. The number block_nchars of characters in the blocks must be valid for the encoding.

Parameters:
  • n (int or None, optional) – the number of samples

  • block_nchars (int) – the number of characters in a block

  • encoding (BlockBaseEncoding) – block encoding for which the char blocks must be valid

Return type:

Iterator[str]

rand_bytes

rand_bytes(n=None, *, encoding=None)[source]

Generates a stream of random bytes objects. If a number n is given, that number of samples is yelded. If an encoding encoding is given, only bytes valid for that encoding are yielded.

Example usage:

>>> my_random_bytes = list(random.rand_bytes(4, encoding=base10))
>>> [list(b) for b in my_random_bytes]
[[0, 30, 135, 156, 223, 90, 134, 83, 6, 243, 245],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 216, 87, 1, 2],
 [70, 98, 190, 187, 66, 224, 178],
 [0, 96, 63]]
Parameters:
  • n (int or None, optional) – the number of samples

  • encoding (BaseEncoding or None, optional) – optional encoding for which the bytestrings must be valid

Return type:

Iterator[bytes]

rand_char

rand_char(n=None, *, alphabet, non_zero=False)[source]

Generates a stream of random characters from the alphabet (one character yielded at a time). If a number n is given, that number of samples is yelded. If non_zero is True, the zero character for the alphabet is not yielded.

Parameters:
  • n (int or None, optional) – the number of samples

  • alphabet (Alphabet or None, optional) – optional alphabet for which the characters must be valid

  • non_zero (bool, optional) – whether to exclude the zero character for the alphabet

Return type:

Iterator[str]

rand_raw_bytes

rand_raw_bytes(n=None, *, min_bytes=None, max_bytes=None)[source]

Generates a stream of random bytes objects. If a number n is given, that number of samples is yelded. The optional min_bytes and max_bytes parameters can be used to set a minimum/maximum length for the bytes objects: if None, the values are fetched from get_options.

Parameters:
  • n (int or None, optional) – the number of samples

  • min_bytes (int or None, optional) – the minimum length for the bytestrings

  • max_bytes (int or None, optional) – the maximum length for the bytestrings

Return type:

Iterator[bytes]

rand_str

rand_str(n=None, *, encoding=None, alphabet=None)[source]

Generates a stream of random strings. If a number n is given, that number of samples is yelded. Exactly one of encoding or alphabet must be given: - if an encoding is given, only strings valid for that encoding are yielded - if an alphabet is given, only strings valid for that alphabet are yielded

Example usage:

>>> my_random_strings = list(random.rand_str(4, encoding=base32))
>>> my_random_strings
['2CQ7ZT6WNI', 'IGQJTGA', 'V6GW3UN64QDAFZA7', 'PUEMOPJ4']
Parameters:
  • n (int or None, optional) – the number of samples

  • encoding (BaseEncoding or None, optional) – optional encoding for which the strings must be valid

  • alphabet (Alphabet or None, optional) – optional alphabet for which the bytestrings must be valid

Raises:
  • ValueError – unless exactly one of encoding or alphabet is specified

  • ValueError – if an instance of a an unsupported (i.e. custom) base encoding subclass is passed to encoding

Return type:

Iterator[str]

reset_options

reset_options()[source]

Resets random generation options to their default values.

Return type:

None

set_options

set_options(*, seed=None, min_bytes=None, max_bytes=None, min_chars=None, max_chars=None)[source]

Permanently sets random generation options:

seed: int           # set new random number generator, with this seed
min_bytes: int      # min length of `bytes` value
max_bytes: int      # max length of `bytes` value
min_chars: int      # min length of `str` value
max_chars: int      # max length of `str` value
Parameters:
Return type:

None