bases.random
Functions to generate random data.
default_options
get_options
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.
rand_block_chars
rand_bytes
- rand_bytes(n=None, *, encoding=None)[source]
Generates a stream of random
bytes
objects. If a numbern
is given, that number of samples is yelded. If an encodingencoding
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]]
rand_char
rand_raw_bytes
- rand_raw_bytes(n=None, *, min_bytes=None, max_bytes=None)[source]
Generates a stream of random
bytes
objects. If a numbern
is given, that number of samples is yelded. The optionalmin_bytes
andmax_bytes
parameters can be used to set a minimum/maximum length for thebytes
objects: ifNone
, the values are fetched fromget_options
.
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 ofencoding
oralphabet
must be given: - if anencoding
is given, only strings valid for that encoding are yielded - if analphabet
is given, only strings valid for that alphabet are yieldedExample usage:
>>> my_random_strings = list(random.rand_str(4, encoding=base32)) >>> my_random_strings ['2CQ7ZT6WNI', 'IGQJTGA', 'V6GW3UN64QDAFZA7', 'PUEMOPJ4']
- Parameters:
- Raises:
ValueError – unless exactly one of
encoding
oralphabet
is specifiedValueError – if an instance of a an unsupported (i.e. custom) base encoding subclass is passed to
encoding
- Return type:
reset_options
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