[EN] random

Random numbers in Python use the random class, but microcontrollers don’t support as much randomness as in computer systems, so Micropython supports different commands depending on the chip type. This article discusses how to use random with esp8266 and esp32 microcontrollers, which are instructed to just generate random numbers and configure the random seed as follows.

random

Randomization is an integer random number using seed as the default randomness. For this reason, if the seed is set to the same number, the randomness will have the same value. As an example for the esp8266 microcontroller, but with the difference in the esp8266 and esp32 chips, the random classes of the two are different. The esp32 supports the esp8266 instructions but adds randrange, randint, choice, random and uniform.

esp8266

The esp8266 microcontroller supports two random commands as shown in Figure 1. Set random or seed and randomly n-bit integers.

(Figure. 1 esp8266’s random class)

The random seed setting is used as follows:

random.seed( seed )

An integer random number must specify the number of bits of randomness. For example, if you want to take a random value in the range 0-100, you must use the value n of 7 because it supports randomization from 0 to 127, etc.

result = random.getrandbits( n )

An example code of use is demoRandom and an example output is shown in Figure 2. It is found that setting the seed to the same value results in the same result each time of the randomization. The (nth random) number of numbers has the same value.

# demoRandom
import random

print("สุ่มตัวเลขขนาด 8 บิต")
for i in range(2):
    msg = "สุ่มครั้งที่ {} : ".format(i+1)
    for n in range(8):
        msg += str(random.getrandbits(8))
        msg += ","
    print(msg)
print("สุ่มตัวเลขขนาด 8 บิต ตั้ง seed เป็น 10")
for i in range(2):
    random.seed(10)
    msg = "สุ่มครั้งที่ {} : ".format(i+1)
    for n in range(8):
        msg += str(random.getrandbits(8))
        msg += ","
    print(msg)

(Figure. 2 Result of demoRandom)

esp32

The command in the random class of esp32 is shown in Figure 3.

(Figure. 3 esp32’s random class)

To randomize decimal numbers in the range 0.0 to 1.0, use the following command:

result = random.random()

If you want to randomize decimals in a given range of values, use the uniform command in this format.

result = random.uniform( start, stop )

There are two types of integer random numbers: random in a given range of values and randomize the value range by specifying the step value or the difference of each random number set as follows:

result = random.randint( start, stop )
result = random.ranrange( start, stop, step )

For random from tuple or list data, the following command can be used to randomly select.

result = random.choice( list )

Example program demoRandomEsp32 is an implementation of random commands that are only supported in esp32, and an example from 2 runs is shown in Figure 4.

# demoRandomEsp32
import random

print("random.random() ...............: {}".format(random.random()))
print("random.uniform(5,10) ..........: {}".format(random.uniform(5,10)))
print("random.randint(5,10) ..........: {}".format(random.randint(5,10)))
print("random.randrange(5, 20, 4) ....: {}".format(random.randrange(5, 20, 4)))

mcu = ("mcs51","pic","avr","stm32","esp8266","esp32")
print("random.choice(mcu) ............: {}".format(random.choice(mcu)))
(Figure. 4 Result of demoRandomEsp32)

Conclusion

From the article, you will find that the class instruction support is different for each microcontroller. And each model of Micropython may be different as well. Because developers can modify Micropython’s source code or write C language extensions via Python’s cython that they can be run in Python, so when developing, they must first ensure that the commands that Requirements are supported with the given options. and likewise, if multiple randomization methods are not needed, the lower cost esp8266 is also an attractive alternative. For the case that requires random values ​​and complex processing, we recommend using ulab. Finally, have fun with programming.

If you want to talk with us, feel free to leave comments below!!

(C) 2020-2021, By Jarut Busarathid and Danai Jedsadathitikul
Updated 2021-10-30