random — generate random numbers¶
This module implements a pseudo-random number generator (PRNG).
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}.
Functions for integers¶
- random.randrange(start: int, stop: int | None = None, step: int = 1) int¶
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¶
Other Functions¶
- random.seed(n: int | None = None, /) None¶
Initialise the random number generator module with the seed n, which should be an integer. When no argument (or
None) is passed in, the PRNG is initialised with a true random number from the hardware random number generator.
- random.choice(sequence: Any) Any¶
Chooses and returns one item at random from sequence (tuple, list or any object that supports the subscript operation).