Skip to main content

True Random Numbers in Scratch

Profile picture for user DaveBakker
Submitted by DaveBakker on Fri, 02/16/2018 - 01:06

We can create a way to make true random numbers in Scratch using the PocketLab Voyager's light sensor and a lava lamp. Sounds crazy? Not really, there is actually a US patent for such a system! It turns out that on their own, computers are not good at generating true random numbers, therefore to make true random numbers using a computer you need an external source of randomness.

Random numbers in scratch with a lava lamp

Computer random number generators are called "pseudo-random" because the numbers only look random. The state of the art methods to generate true random numbers use noise from natural phenomenon, for example atmospheric noise or counts from a geiger counter. You can read an interesting story about how got started building a true random number generator from a cheap radio receiver which you can access on their web page.

Pocketlab makes science easy and fun

If you have a lava lamp at home, you can easily replicate this project. Still think it sounds silly? Did you know that a company called Cloudflare writes software that protects about 1/3 of the world's websites from hackers (DDoS) and they base their cryptography on a camera pointing at a wall of lava lamps? No kidding, it really works.

Lava lamps in the Cloudflare lobby courtesy of @mahtin

If we want to get fancy, we can call our project an "Enhanced Optical-Hybrid Linear Congruential Generator". That may sound complicated, but let's break it down. The computer code we are going to write in Scratch is called a 'Linear Congruential Generator' (LCG for short - don't worry, it's only a small block of code) and the 'Enhanced Optical-Hybrid' part only means that we are going to input data from the lava lamp into the LCG to reset a part of the code that is called the 'seed'.

Let's get started. The LCG equation can be found lots of places, it has been in academics for years. This is from Wikipedia:

LCG from Wikipedia

If you stop to think a moment, this is how your computer generates random numbers - by a formula. If you start with the same seed, then you will get the same list of numbers. They may look random, but they are hardly true random numbers! Consider that a hacker only needs to know what kind of computer system you used to generate random number, then he or she would have a chance of guessing what random number your computer would generate next. Now imagine if you were writing code for the state lottery! This becomes a big problem.

We're going to make the hacker's job much harder with our Scratch program, so let's get going. First step, make the variables:

Variables for lava-rand

Next, let's implement the equation:

LCG equation

That is basically all the math code! So where do we get the values for the multiplier, increment, and modulus? From Wikipedia of course. Scroll down to the table, and you will see different programming languages all use different parameters. That's interesting already - it means that different programming languages will produce different pseudo-random numbers.

All we have to do now is get the light intensity data from our lava lamp, and use that to reset the seed. I've taped the PocketLab Voyager to the side of a lava lamp:

lava lamp and voyager generate random numbers in scratch

Here is a graph of light sensor data collected from the PocketLab Voyager. You can see that the value is pretty unpredictable, that is exactly what we want - it will reset the seed in a very unpredictable way and the LCG math will generate a series of pseudo random numbers from that seed. The idea is that, even if someone knows we are using an LCG, and knows the parameters, still they cannot predict the series of numbers because the input is unpredictable.

Lava lamp data

Here are the code blocks for the whole program. A couple things to note: First, I've used the parameters from Microsoft Visual Basic because it has a smaller modulus, and also there are a couple 'round' functions. These are to get around what appear to be bugs in Scratch's math, and also limitations on the digits of Scratch floating point arithmetic. Someone with more intimate knowledge of Scratch could surely optimize this better. One more note, there doesn't appear to be a 'square' function in Scratch, so you can use the natural log to get 2^24:

Final lava-rand program

There you have it - a pretty good way to generate truly random numbers from Scratch! There is a lot you can do with this, like trying different parameters from different software systems, or actually printing the numbers and doing tests for randomness. Maybe you can come up with a different random source for the seed using your PocketLab sensors. You can even improve on the math with some more research into how Scratch math operators work. Have fun!

Lava lamp Scratch random number generator

To access this free lesson, please sign up to receive communications from us: