Exploring Randomness 2: Seeds
Randomness in a computer can never truly be random. You can get close, but theoretically a computer’s exact state can always be replicated to recreate the values generated. In cases where you need cryptographically secure randomness, entropy (unpredictability) is required; be that from wiggling your mouse while a key is generated, or something like the famous wall of lava lamps. That being said…
Sometimes you do want the random output to be exactly the same each time.
Introducing seeds. You can initialise a random number generator with a value that will ensure that the sequence of random numbers generated will be the same every time the program is ran. This is useful for things like testing, debugging, or generating predictable outputs in games and simulations.
Here’s how you might use a seed in Python. Notice that the result is always the same. (I used Brython to run this code in the browser.)
import random
random.seed(42) # Sets the seed to 42
# Prints 5 random numbers between 0 and 100
for i in range(5):
print(random.randint(0, 100), end=" ")
Take the popular video game Minecraft, for example. Upon making a new world, you’re asked to optionally input a seed which determines how the game generates its environment. If you’re interested, here’s a link to a list of seeds with images of their subsequent worlds. Check out “Pink and White” (seed 5063885805507972583, version 1.21) on that page, then come back and compare the screenshot to this one I took myself:
Interestingly, the trees appear to have generated a little differently, but you can see the terrain is otherwise identical. Perhaps I am on a different version of the game to the author of the article.
Seeds in music
In the context of computer music, seeded randomness can be used to generate melodies, rhythms, virtually anything that you may wish to stay identical each time.
A great example of this is with drum loops. In class I wrote a ‘Rand-O-Drummer’ Max for Live device. As the name suggests, it generates a drum loop with a seed, and it plays over and over. I won’t go into great detail here as I have covered it in a previous post, but you can read all about it here if you wish.
If you can’t already tell, I really love both computer science and music. It makes me so happy to know that there are some really interesting topics that can be applied to both fields, and I’ll continue to write about them here.
As always, thanks for reading. More to come soon.