Join Tammy Coron for an in-depth discussion in this video Working with random sources, part of Programming iOS 9 and GamePlayKit.
- When it comes to the classes surrounding randomization, there are two things you need to know. There are Random Sources and Random Distributions. Random Sources in game play kit, are created using GKRadomSource or one of its sub classes. They are independent and deterministic. However, as you'll see momentarily, it is possible to create a shared Random Source meaning one that is neither deterministic nor independent. Let's take a look at how that would work.
This is a playground that I created specifically for this course. What's nice, is when we're done going through this code you'll have this playground along with all the other other code I created in your exercise files. So, if you ever are curious as to how something works you'll notice that this code and the code that will come up later in the course is heavily commented. You shouldn't have any questions as to what something does. Let's take a look at the Shared Random Source I've created here on line 14.
Line 14, I'm using the Shared Random function from GKRandomSource to create a shared Random Source. Now, as I said, a Shared Random Source is neither deterministic nor independent. So, you really wouldn't want to use a Random Source like this for anything more than trivial game play. Once you've created your Random Source, in order to generate a random number, you can use one of the many functions that game kit has provided for you. On line 17, I'm using a next int function.
That function will return a number that's roughly negative two billion to roughly positive two billion. As you can see here on right this is my results view in my playground. This number is returning a pretty high number, roughly in the two billion mark. If I were to run this code again by executing the playground it's now going to return a different number. This one is about one billion. That's a pretty large number, most of the time, you're not going to need a number that large.
Luckily, game play kit , it gives you another function nextIntWithUpperBound as you can see here in line 20. With nextIntWithUpperBound, I can limit the maximum number returned. In this case, I'm limiting my number to 100. So, what I'm telling game play kit here, please give me a number between zero and 100. Right now, it's giving me the number of 27 as my random number. If I wanted to increase that range, from let's say, zero to 205, I can simply type in for the parameter 206.
Now, when game play kit goes and returns that number to me, it's pulling from a number range of zero to 205. See, now it's giving me 151. Numbers are not the only thing that game play can give back to as random values. You can also use game play kit for random true/false values, as I've done here on line 23. using the next Bol function. So, next Bol will give me either a true or false value.
Here I'm getting true. If I were to rerun this playground, I'm getting false. That was actually kind of cool, I was hoping I wasn't going to get true again, so that's awesome. As you can see, it will either give me a true or false. But still, there's more. It's not just ints and nextintWithUpperBound, the next Bols. Another option is to use the next uniform function. This function returns a float value between zero and one. In this case, it's returning 0.342911.
So, again there are many functions that you've got at your disposal with game play kit to return random values. So, let's cut back and see what other types of Random Sources we can generate. Each of the examples I just showed you uses a non-deterministic and non-independent source. As I said, this is one of reasons why these should only be used for trivial game play mechanics. If your game requires a deterministic and independent source, as most games will, you'll have three options from which to choose.
The first is, GKARC4RandomSource. It uses the ARC4 algorithm and is the most commonly used source. It has a nice balance between speed and randomness. Next, is GKMerseenTwisterRandomSource. This is a little slower but more random. Finally, there is GKLinearCongruentialRandomSouce. This one, as you might have guessed, is faster but less random. Let's take a look at how each one of these looks in code.
On line 33, you'll see that I'm creating a random source using GKRandomSource directly. Now, I don't recommend you do this in your production code. I recommend that you create the random source directly by using whichever random source you decide you want to use. Whether it be ARC4, or Mersenne, or Linear. On line 37, I'm creating a random source using the ARC4 random source. On line 41, I'm creating a random source using the MersenneTwisterRandomSource.
On line 45, I'm creating a Random Source using the Linear Random Source. You'll notice that each one of these, I'm using the nextintWithUpperBound function. What's great about Random Sources, is that you've got access to all of the same functions regardless which Random Source you choose. Here, I'm using NextintWithUpperBound. I can just as well use Nextint it doesn't matter. You've got access to all of the same functions regardless of the Random Source that you use. One word of caution. If you plan to use the ARC4 Random Source, as I have done so in line 52, you run the risk of having certain sequences repeating.
You can correct that problem by using the drop values with count function. You do need to pass in a value of greater than 768 typically 1024 would work. Then, when you return your nextintWithUpperBound, as I've done here, the chances of having a repeat sequence are less than if I didn't use drop values with count. A note about repeating and recreating sequences. you can also reseed your Random Sources, as I've done here on line 58.
Now, the process for doing that, is find the Random Source that you want to recreate. In this case, on line 57, I'm using my Random Source that I created on line 52, I'm grabbing its seed value which is property of the Random Source, and I'm storing that in a variable. I'm then using that variable as its seed when I generate the next Random Source. Something interesting to note here, if you take a look at the results panel, you'll notice that the first Random Source is returning a value of 53.
Whereas the second Random Source is returning a value of 81. Now, I just got done telling you that I can recreate a sequence by using the same seed value from another Random Source. So, you may be thinking to yourself, But wait those numbers are different. Do you remember when I said the chance of repeating values with a Random Source for the ARC4 Random Source if you didn't want to repeat values, you would need to use the drop values with count? Watch what happens when I comment this line out.
Now, you'll notice that the number being returned from the first Random Source is 42 and the same number 42, is being returned for the second Random Source. That's because the drop values with count value is going to have an impact on the number that's returned. So again, Random Sources can be created using three different classes. GKARC4RandomSource, GKMersenneTwisterRandomSource and GKLinearCongruentialRandomSource.
- What is GameplayKit?
- Working with random sources and distributions
- Adding randomization to games
- Working with entities and components
- Defining and creating states and behaviors
- Designing a game for AI
- Adding pathfinding to avoid obstacles
- Using the agent system to manage player movement
- Creating a simple rules system