CS170: Programming for the World Around Us - The micro:bit and Pseudorandom Number Generators

Activity Goals

The goals of this activity are:
  1. To introduce the microbit as a programming device
  2. To explain why computers cannot generate truly random numbers
  3. To explain how computers generate pseudorandom numbers
  4. To explain the role of seed values in pseudorandom number generators (PRNG)
  5. To provide examples of mechanisms that computers use to seed PRNGs
  6. To develop an algorithm to generate pseudorandom numbers with a computer
  7. To implement an algorithm using the micro:bit blocks language
  8. To create and use a GitHub account to save your micro:bit MakeCode project
  9. To create and manipulate variables in a computer program
  10. To implement a mathematical formula using code

Supplemental Reading

Feel free to visit these resources for supplemental background reading material.

The Activity

Directions

Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity, and compare with your group to prepare for our whole-class discussion. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.

Model 1: Random Numbers

From where we stand the rain seems random. If we could stand somewhere else, we would see the order in it.
Tony Hillerman, Coyote Waits

Questions

  1. Pick three numbers from 1 to 10 at random, and write them on the board.
  2. What do you notice about the distribution of those numbers?
  3. How did you come up with those numbers? How did you ensure they were truly quot;random?"
  4. What does it mean to be "random?"

Model 2: Computers and Random Numbers

Questions

  1. How do computers pick numbers "at random?"
  2. How do they decide when to spawn certain "random" events?
  3. Is this video game player really good at the game?

Model 3: Pseudorandom Number Generation

Pseudonumber Random Number Generator (PRNG) formula
Linear congruential generator visualisation

Questions

  1. The Linear Congruential Generator is one way to generate a pseudo-random number computationally. Choose a starting value and generate 3 random numbers using this formula.
  2. What would happen if two people used the same starting value (called a "seed")?
  3. How might video games generate a seed?

Model 4: Introduction to the micro:bit

Micro-bit v1 & v2.JPG: Creative Commons Zero, Public Domain Dedication
micro:bit devices are "programmable units" that have:
  • electrical connections
  • LED output display
  • radios
  • compass
  • accelerometer
  • speakers
  • light sensors
  • buttons

Questions

  1. Using the micro:bit, create variables for a, b, c, and X: call them multiplier, adder, modulus, and current.
  2. Give them any values you want by setting their values in on start.
  3. When the user presses the A button, set the value of the X variable (current) using the formula, and show X on the screen.
  4. Is it ok that the X variable appears on the left and the right side of the equals sign? Why or why not?
  5. On start, show the value of X on the screen.
  6. When you're done, click on the Python tab at the top. What do you notice about each block from the original program?
  7. Save your project to a GitHub account; you can create one for free!

Pairing the Microbit to your Computer

You can connect the micro:bit to your computer by plugging in the USB cable. However, some computers do not have a compatible mac cable. If you don’t have an adapter, your computer may support Bluetooth, a wireless protocol that you can use to connect to the micro:bit. You can do this by holding down the A and B buttons of the micro:bit, and, while holding them down, press and release the reset button. You’ll see the words “PAIRING MODE” appear on the micro:bit display. Open a bluetooth application on your computer (on the Mac, one such program is called LightBlue), and select the BBC micro:bit to pair. The micro:bit will prompt you to press the A button and a 6-digit code will appear on the screen. Type that code on your mac and you should have access to the storage of the micro:bit! You can download your code from Makecode, and then drag the .hex file from your Downloads directory to the micro:bit disk. It will restart and launch your app automatically. Here is a video describing the process on a Mac, although the process is similar on other Bluetooth enabled computers.

Submission

I encourage you to submit your answers to the questions (and ask your own questions!) using the Class Activity Questions discussion board. You may also respond to questions or comments made by others, or ask follow-up questions there. Answer any reflective prompt questions in the Reflective Journal section of your OneNote Classroom personal section. You can find the link to the class notebook on the syllabus.