urandom – random number generation

This module implements a subset of the corresponding CPython module, as described below. For more information, refer to the original CPython documentation: random

Difference to CPython

The pseudorandom algorithm used in Micropython is the Yasmarang algorithm , instead of the Mersenne Twister used in CPython.

This module allows generation of pseudo-random numbers, including setting of a seed. (These are probably not suitable for cryptographic purposes.)

On the RI5, the random generators do work without calling seed(). It’s not immediately clear what seed is used by the module in this case, but there’s no obvious repetition in outputs. If you want an explicit seed whose value is relatively difficult to predict, consider something like utime.ticks_cpu().

Functions

urandom.seed(a)

Initialize the pseudorandom number generator with integer a.

urandom.randrange(stop)
urandom.randrange(start, stop[, step])

Return a randomly selected element from range(stop) or range(start, stop, step).

urandom.randint(a, b)

Return a random integer N between a and b (inclusive).

urandom.getrandbits(n)

Return an integer with n random bits.

urandom.choice(seq)

Return a random element of the given sequence.

urandom.random()

Return a float between 0 and 1 (not including 1).

urandom.uniform(a, b)

Return a float between a and b (b may or may not be included depending on floating-point rounding).