35: Random Numbers Start With A Seed.
Take Up Code - En podcast af Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Kategorier:
Programming involves giving specific instructions but sometimes you want the ability to introduce random behavior. Maybe you want to simulate how a human character sometimes does things in a different order or takes a different path. Or maybe you want the computer to select various items from a collection. Or maybe you want to simulate the randomness of rolling dice. This episode explains how to do this. Your programming language likely gives you classes or methods to generate random values but while the sequence of values might look random, they’re actually pseudo-random. If you use the same seed value to start the sequence in two different random number generators that are each using the same techniques to generate random numbers, then you’re going to get the same sequence. This could lead to your customer playing a great first game but then getting bored because the same things happen each time. You really want to use different seeds when using random numbers to determine the computer’s responses to the player’s actions. But if you’re also using random numbers to generate your world details, then you might actually want the same seed to be used so that the world is the same when the player restarts the same game. Listen to the full episode about random numbers, or you can also read the full transcript below. Transcript The first thing to understand is that a computer processor cannot by itself produce anything random. That’s just not how it works. It follows specific instructions that some programmer has written. Even the code that produces random numbers is subject to these conditions. If you ask for a random number from zero through nine, you’ll get a number, let’s say eight. There’s not much you can conclude about the randomness of a single number. If you had received zero instead of eight, would that mean your random number generating code is any more or less random? No. The only thing you can judge is how well the numbers are distributed across many calls asking for a random number. For a uniform distribution of one thousand requests for a random number, you should expect to get about 100 results of each digit. You may get only 90 twos and that could be okay. At least it’s close to 100. If you got exactly 100 of each digit, then that would be suspicious, but again, it may be okay. If you did this same test and always get exactly 100 results of each digit, then there’s probably something wrong. And the same thing goes for low results. If you ask for one thousand results and get only 10 threes, then there’s probably something wrong. How does the computer create what looks like a series of random numbers when it’s really just following exact instructions? It uses what’s called a seed which the random number generator code expects you to provide. If you give it a seed that’s unique, then you’ll get good results. This doesn’t mean that any particular seed value is bad or that there are some seed values you should avoid. They’re all good. What you don’t want to do is provide the same seed. Here’s why. Let’s say that you roll a six sided dice and get one. So you think that’s a good random number and use it for your seed. And let’s say that the first five random numbers generated are 9, 2, 5, 1, and 0. At some later point, you need some more random numbers so you roll a dice again and this time get six. You create a new random number generator and get five different value. Everything’s good so far. Then you need some more random numbers and end up using the value one for the seed. Your random numbers are again 9, 2, 5, 1, and 0. This is when you realize that what looked like a perfectly good sequence of random numbers will be duplicated if you use the same seed. It’s the seed that determines the order that the numbers will be returned. Give two random number generators that are