random — Random Number Generation

This module is based on the random module in the Python standard library. It contains functions for generating random numbers.

Functions

random.randint(start, end)

Randomly generate an integer between start and end.

  • start: The starting value of the range, inclusive.

  • stop: The ending value of the range, inclusive.

Example:

>>> import random
>>> print(random.randint(1, 4))
4
>>> print(random.randint(1, 4))
2
random.random()

Randomly generate a float between 0 and 1.

Example:

>>> print(random.random())
0.7111824
>>> print(random.random())
0.3168149
random.unifrom(start, end)

Randomly generate a float between start and end.

  • start: The starting value of the range, inclusive.

  • stop: The ending value of the range, inclusive.

Example:

>>> print(random.uniform(2, 4))
2.021441
>>> print(random.uniform(2, 4))
3.998012
random.getrandbits(size)

Randomly generate a positive integer within the range of 0 to size binary bits.

  • size: Bit size. For example, size = 4 means a random positive integer from 0 to 0b1111; size = 8 means a random positive integer from 0 to 0b11111111.

Example:

>>> print( random.getrandbits(1))  #1 binary bit, range 0~1 (decimal: 0~1)
1
>>> print(random.getrandbits(1))
0
>>> print(random.getrandbits(8))  #8 binary bits, range 0000 0000~1111 1111 (decimal: 0~255)
224
>>> print(random.getrandbits(8))
155
random.randrange(start, end, step)

Randomly generate a positive integer from start to end with increment of step. For example, randrange(0, 8, 2) randomly generates one of 0, 2, 4, 6.

  • start: The starting value of the range, inclusive

  • stop: The ending value of the range, inclusive

  • step: Increment base

Example:

>>> print(random.randrange(2, 8, 2))
4
>>> print(random.randrange(2, 8, 2))
6
>>> print(random.randrange(2, 8, 2))
2
random.seed(sed)

Set the random number seed, usually used in combination with other random number generation functions.

Attention

Random numbers in MicroPython are actually a stable sequence of results derived from a stable algorithm, not a truly random sequence. The seed is the first value from which the algorithm starts computing. So as long as the seed is the same, all subsequent “random” results and their order will be exactly the same.

Example:

import random

for j in range(0, 2):
  random.seed(13)  #Set random seed
  for i in range(0, 10):  #Generate random sequence in range 0 to 10
    print(random.randint(1, 10))
  print("end")

Output:

5
2
3
2
3
4
2
5
8
2
end
5
2
3
2
3
4
2
5
8
2
end

As shown above, the two generated random number lists are identical. You can also generate more random number lists to verify. Additionally, when the seed(sed) function is not used, it is equivalent to not specifying a random seed, resulting in random generation.

random.choice(obj)

Randomly generate an element from object obj.

  • obj: Element list

Example:

>>> print(random.choice("mPython"))
m
>>> print(random.choice("mPython"))
n
>>> print(random.choice([0, 2, 4, 3]))
3
>>> print(random.choice([0, 2, 4, 3]))
3
>>> print(random.choice([0, 2, 4, 3]))
2