Pseudo Random Number Generation with the Microbit
Elementary Statistics (Microbit)
In this activity, we will use the Micro:bit to create a sequence of pseudo-random numbers using a formula.
How do you think the Micro:bit generates a random number for the dice? It is difficult to create a random number with a computer, because computers "compute" values with formulas. These formulas, called pseudo-random number generators, create sequences in which the current value of the function is the input to the next calculation.
Getting Started: Generating Random Numbers
What makes a random number random? Brainstorm a few ideas with the person next to you, and share them out with the rest of the class.
Generally, random numbers should be difficult to guess, and it is preferable that they be uniform. That is, over time, these random number sequences should produce a similar number of values. Imagine rolling a dice that never rolled a 5! You would quickly figure this out; every dice value should occur sometimes, and with equal probability. That is a uniform distribution of values.
Come up with 10 random numbers between 1 and 6 (like rolling a dice) and share them with the person next to you. Don’t write them down, just say them out loud. Your partner can keep track of how many 1’s, 2’s, 3’s, 4’s, 5’s and 6’s you say. When you’re done, switch roles and do this again. How many of each value did you come up with? Are they roughly uniform?
Generating Random Numbers with a Computer
One formula for generating random numbers is the Linear Congruential Generator. It uses this formula:
\(X = (aX + b) (mod \; c)\)
- X is the current random number value
- a is a multiplier that we multiply the random number by
- b is a value that we add to the random number
- c is the "modulus" that we take the remainder of the random number
The new random number X depends on the prior value: you can see this because X is on the left side of the equation and the right side of the equation. Make up some values for a, b, and c, and generate some random numbers on paper using a calculator. You’ll need a starting random number of X: pick any number you like! This is called a seed value, and it determines all the random numbers that this function will generate.
Random Number Generation using the Micro:bit
We will use the Micro:bit to implement this formula and generate random numbers. To do this, we will need an on start block and an on button A pressed block on Makecode.
For beginners, it is a good idea to provide the on start block as starter code using Makecode Classroom.
Within the on start block, let’s set up the variables from the formula. There were 4 of them: a modulus, a multiplier, an adder, and a X random number (which I will call current_random since I prefer descriptive variable names!). Set these to any values you like. I set them as follows, but the numbers themselves are completely made up:
current_random = 371 modulus = 5891 multiplier = 1763 adder = 2565
Now we’re ready to use these variables in our formula to start generating random numbers. Let’s add one more item to on start to actually see the number. Add a show number step that displays the current_random variable.
On Button Pressed
When the A button is pressed, we’ll run the formula to generate a new random number. There are a few ways to do this, but generally, we’ll need to multiply current_random by the multiplier, then add that to adder, and, finally, comptue the remainder after dividing by modulus. The modulus keeps the values from getting too big, since we are always adding and multiplying to make the value bigger. The modulus keeps our random numbers between 0 and the value of modulus.
Of course, you’ll want to see the value on the LED output, so add that show number step that you saw before.
Optional: Random Numbers from 0 to 9
It’s easier to see single digit numbers on the Microbit, but single digit values are hard to use to generate random values using this formula. Rather than change the random number itself, we’ll just show the last digit of the random number on the display. Change both of the show number steps to show the remainder after dividing current_random by 10. I created one extra variable called max_value to store this value 10, so that I could change it easily later (and to help me remember what it was for!).
Here’s my finished product, available at https://www.billmongan.com/random-number-generator/:
Extension: Playing with the Seed Values
How uniform were your random numbers? Hit the A button a few times to generate some pseudo-random numbers, and count how many of each number you see. How uniform are they?
Try changing the modulus, multiplier, and adder, values to ones that are more uniform, or ones that are less uniform. What do you notice about them? For example, advanced students might try larger numbers to obtain more uniformity, while others might choose odd or prime numbers.