Random Drawing
In this lesson we will introduce the ability to draw random colors at random pixels. This allows us to create more natural patterns such as the flicking light of a candle.
The Random Functions
We need a function that will generate a random integer between a range of values. This is called the randint()
function and it takes either one or two parameters. In our case, we can just pass in the minimum and maximum values:
1 |
|
You can read more about MicroPython random functions here
To use the random function we first need to import the function from the library:
1 |
|
Here is a sample loop that sets each pixel to a random color using the wheel() function:
1 2 3 4 5 |
|
Full Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
Exercises
- Instead of updating a single pixel in consecutive sequences, can you also use the
randint()
function to update a random pixel? - What if you have a small fixed list of colors to pick from. How can you use
randint()
to pick a color from this list?