.. _random: :mod:`random` --- Generate random numbers ========================================== This module is based on the ``random`` module in the Python standard library. It contains functions for generating random numbers. Functions --------- .. method:: random.randint(start, end) Randomly generate an integer between start and end. - ``start``: the start value of the specified range, included in the range. - ``stop``: the end value of the specified range, included in the range. Example:: >>> import random >>> print(random.randint(1, 4)) 4 >>> print(random.randint(1, 4)) 2 .. method:: random.random() Randomly generate a floating-point number between 0 and 1. Example:: >>> print(random.random()) 0.7111824 >>> print(random.random()) 0.3168149 .. method:: random.unifrom(start, end) Randomly generate a floating-point number between start and end. - ``start``: the start value of the specified range, included in the range. - ``stop``: the end value of the specified range, included in the range. Example:: >>> print(random.uniform(2, 4)) 2.021441 >>> print(random.uniform(2, 4)) 3.998012 .. method:: random.getrandbits(size) Randomly generate a positive integer in the range of 0 to a size-bit binary number. - ``size``: the bit size. For example, if size = 4, a positive integer is randomly chosen from 0 to 0b1111; if size = 8, a positive integer is randomly chosen 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 11111 (decimal: 0~255) 224 >>> print(random.getrandbits(8)) 155 .. method:: random.randrange(start, end, step) Randomly generate a positive integer in the range from start to end with an increment of step. For example, in randrange(0, 8, 2), the randomly generated number is one of 0, 2, 4, or 6. - ``start``: the start value of the specified range, included in the range - ``stop``: the end value of the specified range, included in the range - ``step``: the increment base Example:: >>> print(random.randrange(2, 8, 2)) 4 >>> print(random.randrange(2, 8, 2)) 6 >>> print(random.randrange(2, 8, 2)) 2 .. method:: random.seed(sed) Specify the random number seed, usually used together with other random number generation functions. .. attention:: The random numbers in MicroPython are actually a stable sequence of results produced by a stable algorithm, rather than a random sequence. The seed is the first value from which this algorithm starts computing. So as long as the seed is the same, all subsequent "random" results and their order will also be exactly the same. Example:: import random for j in range(0, 2): random.seed(13) #specify the random number seed for i in range(0, 10): #generate a random sequence in the range 0 to 10 print(random.randint(1, 10)) print("end") Result:: 5 2 3 2 3 4 2 5 8 2 end 5 2 3 2 3 4 2 5 8 2 end From the above you can see that the two generated random number lists are the same; you can also generate a few more random number lists to check. In addition, when we do not use the seed(sed) function, it is equivalent to not specifying a random seed, so the generation is random. .. method:: random.choice(obj) Function description: randomly generate an element from the object obj. - ``obj``: the list of elements 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