Skip to content

Using Random Numbers

One fun way to spice up our LED strips is to randomly light up a different pixel with a random color. Here is how we do this.

First, We need to import the library that generate random numbers:

1
from urandom import randint

Next, we need to generate a random number within a fixed range:

1
    random_red_brightness = randint(0, 256)

Let's create a program that will briefly light up an random pixel with a random color. To do this we first generate a random location on the LED strip, then we get a random value for the red, green and blue brightness:

1
2
3
4
location = randint(0, NUMBER_PIXELS-1)
    red = randint(0, 256)
    green = randint(0, 256)
    blue = randint(0, 256)

Here is the full program.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 07-random color at a random location
from machine import Pin
from neopixel import NeoPixel
from utime import sleep
from urandom import randint

NEOPIXEL_PIN = 0
NUMBER_PIXELS = 60

strip = NeoPixel(Pin(config.NEOPIXEL_PIN), NUMBER_PIXELS)

while True:
    location = randint(0, NUMBER_PIXELS-1)
    red = randint(0, 256)
    green = randint(0, 256)
    blue = randint(0, 256)
    strip[location] = (red, green, blue)
    strip.write()
    sleep(.5)
    strip[location] = (0, 0, 0)