In this lecture, we'll implement our roll method. And just to remind you of what that looks like on our class diagram. Here you go. I'm going to put our single roll method here in a separate region called methods down at the bottom of the Die class. We start with the access modifier, and just as with fields by the way, if we don't put in access modifier, then our method will be by default, private. And the same applies to properties as well. The next thing we provide for our method is a return type. So sometimes a method will return something, like our deck take top card method from the previous course would return a card object. But sometimes a method doesn't return anything. And that's the case here, we're just going to roll the die, changing the internal state of the die without returning any information to whoever told us to roll. So we're going to use a special keyword that means, I don't return anything. And that special keyword is void. Now we put the method name, and in this case, that's Roll. And we put open parenthesis, close parenthesis, and if in fact we had any parameters like we had for our Die constructor that took in the number of sides, then we would put those parameters here, but we don't have any for the roll method, so we just put open and close parentheses with nothing in between them. And of course the open and close curly braces, for the body of the method. I'll add a comment, rolls the die. And so, to roll the die, we are changing the top side. That's what rolling a die effectively ends up doing, right? Is changing the top side. And of course, it could end up being the same when you just rolled. But that is what we're trying to do. So we're going to try to change topSide, and what I want here is I want the topSide to be a random number between 1 and 6. That means, I need a random number generator, so I'll create one of those. And now I can call the random classes, next method. The version that takes in two integers, the lower bound which will be one in our case, and the max value and that is an exclusive upper bound. The lower bound, the min value we provide is inclusive, so 1 could be one of the numbers that the next method returns to us, but the upper bound is exclusive. I certainly don't want to put 6 here, because I might have a d20. We know that numSides tells how many sides the die has, but because this is an exclusive upper bound, we need to add 1 to it. So the body of our method creates a random number generator, and then generates a random number between 1 and numSides inclusive, and sets topSide to be that number. Back in our testing code, let's roll the die. So we will take the standardDie, and we will tell it to roll itself. And then we can print out the TopSide, and I already have codes to do that up here, so I can just copy and paste. If I run the code, we see the top side is 1 and we also see that we wanted to add a blank line, and let's roll the d20 as well, and our control F5. And as you can see, the top side for the standard die is 3, and the top side for the d20 is 9 at this point. And we're happy. We think everything is working fine, but in this manic depressive life of being a game programmer, I'm about to crush your hopes and dreams by showing you what happens next. So let's actually roll these standardDie multiple times, and we will do the same for the d20 as well. And when I run the code, oh no, I end up with the top side of 2, 2, 2 and then I put the top side of 5, 5, 5 for the d20. Clearly, something is going wrong. To understand what's going wrong, we need to understand a little bit more about how random number generator works. So how do pseudo random number generators work? And why are they called random number generators? Well, the way these work is, we start with a seed, it's just a number. And then we generate a sequence of numbers from that number using some algorithm. Some set of steps that say, how do we get the next number in this sequence of numbers. They're not random numbers, because if we know the seed, and if we know the algorithm, then we can perfectly predict the sequence of numbers that we'll get. In the documentation for the random class in the remarks area, it warns us that different random objects that are created in close succession, will have identical seed values so they will generate identical sequences of random numbers. And in fact, the random class doesn't actually generate random numbers, it generates pseudo random numbers, because it uses a seed, and then it uses an algorithm, and if we know the seed and the algorithm, then we could perfectly predict what that sequence of numbers is and that's not random. Here's what's happening in our code. In our testing code, we are rolling the die very quickly, one time after another. So we are coming to this method in creating a new random object, and then doing some stuff, then coming back and doing it again and doing it again. And it's happening so quickly, that this random number generator is getting the same seed each time. So it's giving us the same first pseudo random number every time. There's an easy fix for this small example, I will take this, and I will make it a field instead. So now, instead of creating a new random number generator every time a roll is called, I just create one once when the die object is created, and that should solve our problem. And it does, as you can see. Now the d20 actually got two 4s in a row, and that's just how random this works. So that's a fact of life of generating random numbers. And here the standard die got 4 in a row twice. So random numbers doesn't mean you get each number ones, sometimes generating random numbers just as flipping a coin multiple times might give you heads multiple times in a row before you get a tails, that's just how probability works. And that's the implementation of our roll method in the Die class. To recap, in this lecture we learned how to implement a method. We saw that access modifiers also apply to methods. We learned how pseudo random number generators work. And while it was inconvenient for us here to have the problem of the same sequences of numbers with the same seed, it's actually really useful when we need to debug a game that has randomness in it, and we know what seed we started with, because we can perfectly replicate the problem we ran into, by starting the random number generator with the same seed that we had when we identified the problem. We did not use a robust game-level solution to our die problem of random number generators with the same seed. We moved it to be a little less onerous, but we can fix it even better by using a single random number generator for our entire game. And that's really the robust solution, so that anyone who needs a random number, just goes to that game level random number generator to get one. And finally, even though I haven't said it yet, this is the conclusion of the implementation of our Die class.