Warning:

To protect the digital assets of creators and enhance system security and stability, the CyberBrick Multi-Function Core Board does not support firmware flashing with any third-party tools! If you need to restore the Multi-Function Core Board to its factory state, please wait for the official recovery tool to be released by CyberBrick. If you proceed to flash the firmware using third-party tools, the firmware of the Multi-Function Core Board will be permanently damaged and cannot be recovered. CyberBrick will not be responsible for any consequences resulting from such operations.

This documentation is adapted from the official MicroPython project. The CyberBrick team has extended the source code with custom interfaces and features to our hardware and application needs.

In addition to these enhancements, certain built-in MicroPython interfaces have been intentionally disabled to protect the system's integrity, and ensure the overall security and reliability of the device. This also facilitates content protection for creators' intellectual property, where applicable.

Portions of the content are derived from the official MicroPython documentation and have been included here under its open-source license to provide users with a consistent and enriched development experience tailored to the CyberBrick platform.

random – generate random numbers

This module implements a pseudo-random number generator (PRNG).

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

Note

The following notation is used for intervals:

  • () are open interval brackets and do not include their endpoints. For example, (0, 1) means greater than 0 and less than 1. In set notation: (0, 1) = {x | 0 < x < 1}.

  • [] are closed interval brackets which include all their limit points. For example, [0, 1] means greater than or equal to 0 and less than or equal to 1. In set notation: [0, 1] = {x | 0 <= x <= 1}.

Note

The randrange(), randint() and choice() functions are only available if the MICROPY_PY_RANDOM_EXTRA_FUNCS configuration option is enabled.

Functions for integers

random.getrandbits(n)

Return an integer with n random bits (0 <= n <= 32).

random.randint(a, b)

Return a random integer in the range [a, b].

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

The first form returns a random integer from the range [0, stop). The second form returns a random integer from the range [start, stop). The third form returns a random integer from the range [start, stop) in steps of step. For instance, calling randrange(1, 10, 2) will return odd numbers between 1 and 9 inclusive.

Functions for floats

random.random()

Return a random floating point number in the range [0.0, 1.0).

random.uniform(a, b)

Return a random floating point number N such that a <= N <= b for a <= b, and b <= N <= a for b < a.

Other Functions

random.seed(n=None, /)

Initialise the random number generator module with the seed n which should be an integer. When no argument (or None) is passed in it will (if supported by the port) initialise the PRNG with a true random number (usually a hardware generated random number).

The None case only works if MICROPY_PY_RANDOM_SEED_INIT_FUNC is enabled by the port, otherwise it raises ValueError.

random.choice(sequence)

Chooses and returns one item at random from sequence (tuple, list or any object that supports the subscript operation).