7. Random pixels
7.1. Random pixel and random brightness
7.2. Random pixel randint
- random.randint(a, b)
Return a random integer from a to b, including both.
from microbit import *
import random
while True:
random_brightness = random.randint(1, 9)
random_x = random.randint(0, 4)
random_y = random.randint(0, 4)
display.set_pixel(random_x, random_y, random_brightness)
sleep(50)
display.clear()
Tasks
Modify the code to restrict the brightness to 1 to 5, and the pixels to the central 9 pixels.
Modify the code to restrict the brightness to 1 to 5, and the pixels to the central 9 pixels.
from microbit import *
import random
while True:
random_brightness = random.randint(1, 5)
random_x = random.randint(1, 3)
random_y = random.randint(1, 3)
display.set_pixel(random_x, random_y, random_brightness)
sleep(50)
display.clear()
7.3. Tuple unpacking using an asterisk in a function call
from microbit import *
import random
def rand_pix():
random_brightness = random.randint(1, 9)
random_x = random.randint(0, 4)
random_y = random.randint(0, 4)
return (random_x, random_y, random_brightness)
while True:
display.set_pixel(*rand_pix())
sleep(50)
display.clear()
Tasks
Modify the function, rand_pix(), to restrict the brightness to 4 to 7, and the pixels to the bottom 2 rows.
Modify the function, rand_pix(), to restrict the brightness to 4 to 7, and the pixels to the bottom 2 rows.
from microbit import *
import random
def rand_pix():
random_brightness = random.randint(4, 7)
random_x = random.randint(0, 4)
random_y = random.randint(3, 4)
return (random_x, random_y, random_brightness)
while True:
display.set_pixel(*rand_pix())
sleep(50)
display.clear()
7.4. Tuple unpacking in multiple assignment
from microbit import *
import random
def rand_pix():
random_brightness = random.randint(1, 9)
random_x = random.randint(0, 4)
random_y = random.randint(0, 4)
return (random_x, random_y, random_brightness)
while True:
random_x, random_y, random_brightness = rand_pix()
display.set_pixel(random_x, random_y, random_brightness)
sleep(50)
display.clear()
Tasks
Modify the function, rand_pix(), to restrict the brightness to 3 to 6, and the pixels to the top 2 rows.
Modify the function, rand_pix(), to restrict the brightness to 3 to 6, and the pixels to the top 2 rows.
from microbit import *
from microbit import *
import random
def rand_pix():
random_brightness = random.randint(3, 6)
random_x = random.randint(0, 4)
random_y = random.randint(0, 1)
return (random_x, random_y, random_brightness)
while True:
random_x, random_y, random_brightness = rand_pix()
display.set_pixel(random_x, random_y, random_brightness)
sleep(50)
display.clear()
7.5. Random pixel randrange(stop)
- random.randrange(stop)
Return a randomly selected integer from zero and up to (but not including) stop.
from microbit import *
import random
def rand_pix():
random_brightness = random.randint(1, 9)
random_x = random.randrange(5)
random_y = random.randrange(5)
return (random_x, random_y, random_brightness)
while True:
random_x, random_y, random_brightness = rand_pix()
display.set_pixel(random_x, random_y, random_brightness)
sleep(50)
display.clear()
Tasks
Modify the function, rand_pix(), to restrict the brightness to 3 to 6, and the pixels to the left 3 columns.
Modify the function, rand_pix(), to restrict the brightness to 3 to 6, and the pixels to the left 3 columns.
from microbit import *
import random
def rand_pix():
random_brightness = random.randint(3, 6)
random_x = random.randrange(3)
random_y = random.randrange(5)
return (random_x, random_y, random_brightness)
while True:
display.set_pixel(*rand_pix())
sleep(50)
display.clear()
7.6. Random pixel randrange(start, stop)
- random.randrange(start, stop)
Return a randomly selected integer from start and up to (but not including) stop.
from microbit import *
import random
def rand_pix():
random_brightness = random.randint(1, 9)
random_x = random.randrange(1, 4)
random_y = random.randrange(1, 4)
return (random_x, random_y, random_brightness)
while True:
random_x, random_y, random_brightness = rand_pix()
display.set_pixel(random_x, random_y, random_brightness)
sleep(50)
display.clear()
Tasks
Modify the function, rand_pix(), to restrict the brightness to 1 to 3, and the pixels to the right 3 columns in the bottom 4 rows.
Modify the function, rand_pix(), to restrict the brightness to 1 to 3, and the pixels to the right 3 columns in the bottom 4 rows.
from microbit import *
import random
def rand_pix():
random_brightness = random.randint(1, 3)
random_x = random.randrange(2, 5)
random_y = random.randrange(1, 5)
return (random_x, random_y, random_brightness)
while True:
display.set_pixel(*rand_pix())
sleep(50)
display.clear()
7.7. Random pixel randrange(start, stop, step)
- random.randrange(start, stop, step)
Return a randomly selected element from start and up to (but not including) stop in steps of step.
from microbit import *
import random
def rand_pix():
random_brightness = random.randint(1, 9)
random_x = random.randrange(0, 5, 2)
random_y = random.randrange(0, 5, 2)
return (random_x, random_y, random_brightness)
while True:
random_x, random_y, random_brightness = rand_pix()
display.set_pixel(random_x, random_y, random_brightness)
sleep(50)
display.clear()
Tasks
Modify the function, rand_pix(), to restrict the brightness to 1 to 3, and the pixels to columns 1 and 3 and the rows to 1 and 3.
Modify the function, rand_pix(), to restrict the brightness to 1 to 3, and the pixels to columns 1 and 3 and the rows to 1 and 3.
from microbit import *
import random
def rand_pix():
random_brightness = random.randint(1, 3)
random_x = random.randrange(1, 4, 2)
random_y = random.randrange(1, 4, 2)
return (random_x, random_y, random_brightness)
while True:
display.set_pixel(*rand_pix())
sleep(50)
display.clear()
7.8. Random pixel random.choice
- random.choice(seq)
Return a random element from the non-empty sequence
seq
such as a list or tuple.
from microbit import *
import random
while True:
random_brightness = random.choice((1, 5, 9))
random_x = random.choice((1, 2, 3))
random_y = random.choice((1, 2, 3))
display.set_pixel(random_x, random_y, random_brightness)
sleep(50)
Tasks
Modify the code above to use a function, rand_pix(), to restrict the brightness to 1, 3 or 5, and the pixels to columns 0, 3, and 4 and the rows to 1 and 3.
Modify the code above to use a function, rand_pix(), to restrict the brightness to 1, 3 or 5, and the pixels to columns 0, 3, and 4 and the rows to 1 and 3.
from microbit import *
import random
def rand_pix():
random_brightness = random.choice((1, 3, 5))
random_x = random.choice((0, 3, 4))
random_y = random.choice((1, 3))
return (random_x, random_y, random_brightness)
while True:
random_x, random_y, random_brightness = rand_pix()
display.set_pixel(random_x, random_y, random_brightness)
sleep(50)
7.9. Random Pixel rows and columns
from microbit import *
import random
def rand_x():
return random.randint(0, 4)
while True:
for y in range(0, 5):
display.set_pixel(rand_x(), y, 9)
sleep(200)
display.clear()
Tasks
Write code to set the brightness to 9 for a pixel in each column, with the row being random for each pixel.
Write code to set the brightness to 9 for a pixel in each column, with the row being random for each pixel.
from microbit import *
import random
def rand_y():
return random.randint(0, 4)
while True:
for x in range(0, 5):
display.set_pixel(x, rand_y(), 9)
sleep(200)
display.clear()
7.10. Random Pixels
from microbit import *
import random
def rand_val():
return random.randint(0, 4)
while True:
for _ in range(3):
display.set_pixel(rand_val(), rand_val(), 9)
sleep(200)
display.clear()
Tasks
Modify the code above to produce 10 random pixels at a time.
Change the brightness to 5, and explore how many random pixels are needed so that only 1 to 3 pixels are left turned off.
Modify the code above to produce 10 random pixels at a time.
from microbit import *
import random
def rand_val():
return random.randint(0, 4)
while True:
for _ in range(10):
display.set_pixel(rand_val(), rand_val(), 9)
sleep(200)
display.clear()
Change the brightness to 5, and explore how many random pixels are needed so that only 1 to 3 pixels are left turned off.
About 60 to 75 are needed since the same pixel may be generated more than once.
from microbit import *
import random
def rand_val():
return random.randint(0, 4)
while True:
for _ in range(75):
display.set_pixel(rand_val(), rand_val(), 9)
sleep(200)
display.clear()
7.11. Random Pixels choice Pixel rows and columns lists
from microbit import *
import random
x_vals = [0, 2, 4]
y_vals = [0, 2, 4]
def rand_val(vals):
return random.choice(vals)
while True:
for _ in range(2):
display.set_pixel(rand_val(x_vals), rand_val(y_vals), 5)
sleep(200)
display.clear()
Tasks
Adjust the code to restrict the possible x and y values to the central 3 x 3 square, while showing 3 random pixels at a time.
Adjust the code to restrict the possible x and y values to the central 3 x 3 square, while showing 3 random pixels at a time.
from microbit import *
import random
x_vals = [1, 2 ,3]
y_vals = [1, 2 ,3]
def rand_val(vals):
return random.choice(vals)
while True:
for _ in range(3):
display.set_pixel(rand_val(x_vals), rand_val(y_w), 5)
sleep(200)
display.clear()
7.12. get_pixel and set_pixel
display.get_pixel(x, y) == 0
can be used to check if a pixel is on or off.- get_pixel(x, y)
Return the brightness of pixel at column
x
and rowy
as an integer between 0 and 9.
from microbit import *
def full_screen_on_check():
for y in range(0, 5):
for x in range(0, 5):
if display.get_pixel(x, y) == 0:
return False
return True
from microbit import *
from random import randint
def full_screen_on_check():
for y in range(0, 5):
for x in range(0, 5):
if display.get_pixel(x, y) == 0:
return False
return True
def fill_screen_with_counter():
counter = 0
while True:
counter += 1
x = randint(0, 4)
y = randint(0, 4)
brightness = randint(1, 4)
display.set_pixel(x, y, brightness)
if full_screen_on_check():
return counter
sleep(30)
while True:
new_fill_count = fill_screen_with_counter()
display.scroll(new_fill_count)
sleep(1000)
Tasks
Add code to display the min and max counts obtained in the code above.
Improve the code in answer to task 1, by creating definitions to update the min and max counts, and to display the counts on button pressing. The main loop should look like this:
Improve the code in answer to task 2, by adding a set to keep track of displayed pixels in the function, fill_screen_with_counter(). Number the pixels 0 to 4 in the top row, 5 to 9 in the next row. etc. Check to see if the length of the set is 25 to tell that the screen is full.
counts = [None, None] while True: new_fill_count = fill_screen_with_counter() counts = update_counts(counts, new_fill_count) display_counts(counts, new_fill_count) sleep(1000) display.clear()
Add code to display the min and max counts obtained in the code above.
from microbit import *
from random import randint
def full_screen_check():
for y in range(0, 5):
for x in range(0, 5):
if display.get_pixel(x, y) == 0:
return False
return True
def fill_screen_with_counter():
counter = 0
while True:
counter += 1
x = randint(0, 4)
y = randint(0, 4)
brightness = randint(1, 4)
display.set_pixel(x, y, brightness)
if full_screen_check():
return counter
sleep(30)
min_fill_count = None
max_fill_count = None
while True:
new_fill_count = fill_screen_with_counter()
display.scroll(new_fill_count, delay=60)
if min_fill_count is not None:
min_fill_count = min(min_fill_count, new_fill_count)
else:
min_fill_count = new_fill_count
if max_fill_count is not None:
max_fill_count = max(max_fill_count, new_fill_count)
else:
max_fill_count = new_fill_count
display.scroll(min_fill_count, delay=60)
display.scroll(max_fill_count, delay=60)
sleep(1000)
Improve the code in answer to task 1, by creating definitions to update the min and max counts, and to display the counts on button pressing.
from microbit import *
from random import randint
def full_screen_check():
for y in range(0, 5):
for x in range(0, 5):
if display.get_pixel(x, y) == 0:
return False
return True
def fill_screen_with_counter():
counter = 0
while True:
counter += 1
x = randint(0, 4)
y = randint(0, 4)
brightness = randint(1, 4)
display.set_pixel(x, y, brightness)
if full_screen_check():
return counter
sleep(10)
def update_counts(counts, count):
min_fill_count = counts[0]
if min_fill_count is not None:
min_fill_count = min(min_fill_count, count)
else:
min_fill_count = count
max_fill_count = counts[1]
if max_fill_count is not None:
max_fill_count = max(max_fill_count, count)
else:
max_fill_count = count
return (min_fill_count, max_fill_count)
def display_counts(counts, new_fill_count):
if button_a.was_pressed():
display.scroll(new_fill_count, delay=60)
if button_b.was_pressed():
display.scroll(counts[0], delay=60)
display.scroll(counts[1], delay=60)
counts = [None, None]
while True:
new_fill_count = fill_screen_with_counter()
counts = update_counts(counts, new_fill_count)
display_counts(counts, new_fill_count)
sleep(1000)
display.clear()
Improve the code in answer to task 2, by adding a set to keep track of displayed pixels in the function, fill_screen_with_counter(). Number the pixels 0 to 4 in the top row, 5 to 9 in the next row. etc. Check to see if the length of the set is 25 to tell that the screen is full.
from microbit import *
from random import randint
import utime
def full_screen_check():
for y in range(0, 5):
for x in range(0, 5):
if display.get_pixel(x, y) == 0:
return False
return True
def fill_screen_with_counter():
counter = 0
screen_set = set()
while True:
counter += 1
x = randint(0, 4)
y = randint(0, 4)
brightness = randint(1, 4)
display.set_pixel(x, y, brightness)
screen_set.add(x + y*5)
if len(screen_set) == 25:
return counter
sleep(1)
def update_counts(counts, count):
min_fill_count = counts[0]
if min_fill_count is not None:
min_fill_count = min(min_fill_count, count)
else:
min_fill_count = count
max_fill_count = counts[1]
if max_fill_count is not None:
max_fill_count = max(max_fill_count, count)
else:
max_fill_count = count
return (min_fill_count, max_fill_count)
def display_counts(counts, new_fill_count):
if button_a.was_pressed():
display.scroll(new_fill_count, delay=60)
if button_b.was_pressed():
display.scroll(counts[0], delay=60)
display.scroll(counts[1], delay=60)
counts = [None, None]
while True:
new_fill_count = fill_screen_with_counter()
counts = update_counts(counts, new_fill_count)
display_counts(counts, new_fill_count)
sleep(1)
display.clear()
Note
Sets can be good to use when checking somethings since members of the set cannot be be repeated. Adding a member to a set that already exists has no affect.