Danger

Nothing here should be used for any security purposes.

  • If you need cryptographic tools in a Python environment use pyca.

  • If you need efficient and reliable abstract math utilities in a Python-like environment consider using SageMath.

Random Numbers

This module is imported with:

import toy_crypto.rand

For the most part, the functions here duplicate some utilities from random, but use the cryptographically secure random number generator from secrets.

The rand functions

toy_crypto.rand.choices(population: Sequence, weights: Sequence[float] | None = None, *, cum_weights: Sequence[float] | None = None, k: int = 1) Sequence[source]

Return a k sized list of population elements chosen with replacement.

If the relative weights or cumulative weights are not specified, the selections are made with equal probability.

This is the same as random.choices() except that it uses the the random number generator from :py:mod:1secrets`. Indeed, the implementation is just copied from that source.

Parameters:
Return type:

Sequence[TypeVar(T)]

toy_crypto.rand.random() float[source]

returns a 32-bit float in [0.0, 1.0)

Return type:

float

toy_crypto.rand.randrange(*args: int) int[source]

Like random.randrange(), but uses RNG from secrets.

Parameters:

args (int)

Return type:

int

toy_crypto.rand.shuffle(x: MutableSequence[Any]) None[source]

Like random.shuffle(), but uses RNG from secrets.

Parameters:

x (MutableSequence[Any])

Return type:

None