Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a look at the random number generator class before we get into that.
Let's talk about randomness and computers. Naturally computers are predictable machines.
You give a computer an input and the computer gives you back an output. So how exactly do we get random numbers in computers before we get into that. Let's talk about what makes a good random number generator when coming up with a random number generator we need something that fits three boxes when dealing with game programming. The first is that our random number generator needs to be time performance. The next is that we need to occupy as little space as possible.
When calculating for our random number. And lastly while also being the most important is if our random number generator is impossible to predict and when taking all of that into account we come up with two categories of randomness in computers. You have true random numbers and you have pseudo random numbers. These are two ways our computer is able to produce random numbers when dealing with true random numbers.
Our computer will use a measurement and algorithms system that draws from a non deterministic event. Basically this event is a random or unpredictable event. When we get true random numbers we get a feeling of chance the same feeling you get when you for example row eight dice when playing a game.
Some examples of non deterministic events would be how many seconds it has been since the user has clicked a mouse button.
When a user has first started their computer atmospheric noise and radioactive decay generating true random numbers is not as performance as generating pseudo random numbers at least with the technology we have for now I'm going to leave a link down below in the description if you want to take a look at more information when it comes to true random numbers.
The Web site's gonna be from random dot org and they use atmospheric noise when calculating their random number.
Now for everything else we use pseudo random numbers and basically for pseudo random numbers we just need two things.
One is a seed value which in many cases is just an integer value. And the second is an algorithm. Lucky for us we don't need to create our own pseudo random number generator function instead.
Godot provides us a random number generator class the default seed is just a very large negative number and the algorithm that Godot uses is from the PCG algorithm family.
Let's talk about a problem that I see many beginners face when using pseudo random number generators and Godot and the main problem is that the seed has a default value.
This means that you will have predictable numbers. Basically your results will be the same throughout the course of your game.
And when it comes to random numbers we would at least like the feeling of chance.
Now the solution for this is very simple. You just need to set a new seed value. It's a matter of fact the dough provides that to us with the randomize method. What the randomized method does is it changes the seed value to something different. So when you use the randomized function all you're really doing is grabbing information from your computer. Basically it's a time based seed and so because time is never the same.
Are randomized function will allow us to use a seed that is different every time. As long as we do not have the ability to travel back in time.
Now there are two functions that the random number generator class provides us. The first is the rand eye function which gives us back a integer value between 0 and a very large number. The most important one you may find yourself using most of the time is Randy range function and all that does is it provides you a value between two integers. So for example from zero and a hundred. Also keep in mind that the random number generator also provides you similar functionality when dealing with float values between zero point zero and one point zero.
Let's go ahead and take a look at pseudocode. When dealing with the random number generator class.
First you want to create a class instance using the new method from the random number generator class and next.
And the most important step when you want to get the feeling of randomness is that you must use the random mice function before you do anything else. Lastly you can use whatever function that the random number generator class instance provides in this case I'm using Randi range. And so we're gonna get a value between zero and 100. Now let's look at an example where we forget to use the random minus function do the same thing we create a class instance.
We use the function and if we were to print this to the screen we will print ninety seven every time.
And as you can see here because we are using the default seed every time we run Randy range we're always going to get back the value ninety seven and that's because pseudo random number generators use an algorithm and deceit. And so if the seed value is the same every time we pass it to the algorithm the algorithm is always going to output the same result. So just keep that in mind if you want the same result then go ahead and provide your random number generator with these same seed value.
However if you would like to have different results every time you use your function that the random number generator provides then you must change the seed value every time you run your functions. And lucky for us again Godot has the randomized function that changes the seed based on time and so the value you pass to your algorithm changes every time. Well that's all I have for you in this episode. Thank you so much for joining me. I'm gonna post a link to get hub with the random number generator so please feel free to go ahead and play around with that.
Also check out the link to the trailer page I have a curated list of different tutorials from different YouTube channels you may find interesting. If you have any questions or comments please feel free to leave them in the comments section down below. I look forward to seeing you in the next episode. Have an amazing day