welcome back to my Roblox beginner scripting tutorial series my name is balev and in this episode we'll be discussing about random so I want you to imagine that we have a six-sided die and when we roll This die it's going to land on a number between 1 and six we can't control what number this lands on and it's left completely up to chance as to whether we know what number this is going to land on like if I were to let's say pick a number like four and we were to predict that this die is going to land on the number four we have about a 177% chance that this is actually going to happen and this is the concept of randomizing which applies to game development as well like let's say we're playing pet Simulator X and we're trying to hatch an egg the pet that we get has a random chance of giving us a higher Rarity compared to the normal common pets or The Uncommon pets like if we were to get a legendary or something that has a higher Rarity than if we were to go for something that's simpler or if we were to play an RNG game then it's going to pick out a bunch of different items with different Rarities for us to obtain after we roll something and so that is something that's important to know about when it comes to game development on Roblox leaving stuff up to chance and adding randomization to it and so that's what I'm going to be teaching you how to do inside of this episode which is math. random so what we're going to do is go on the right side of the Explorer we're going to hit the plus sign and insert a script we're going to call the script uh random just like this and we're going to hit enter so I'm just going to delete this and I'm also going to get rid of any additional tabs that we're not not going to be using okay so what is math. random it's basically a function that Roblox has given to us that can return a random number within a range that we provided so the way this is going to be structured is we're first going to make a random number variable so we're just going to say local random number just like this and we're going to set this equal to math so it's going to look like this math is basically the library that we're going to be using that has the random function so then we're going to say do random and this is all in lowercase 2 so uh once we do this then we're going to put open close parentheses and inside of here it takes two numbers so it has a range with a minimum value and a maximum value so whatever minimum value we give it and whatever maximum value we give it Roblox is going to pick a number between that range and it's going to return it to the random number variable as we have over here so let's make the minimum value let's say one and then we're going to separate this with a comma for the second argument which is going to be six and six is included with this as well so it's it's is going to pick a number between 1 and six and so what we're going to do down here is drop a line and we're going to print the random number just like this so whenever we join into the game and we fire this math.
random function it's going to automatically give us a different number Every Time We join into the game but sometimes we can be lucky and Roblox will give us the same number consecutively okay so we're going to go into the game and it says that it picked out two so if we stop it and then play again it's going to pick five and so if we stop it and then play it one more time then it's going to pick pick a different number which is six so you get the idea at this point every single time we call the math. random function with the provided range then it's going to return a random number within that range so that is how we use math. random inside of our scripts but now let's do something interesting with math.
random so what if we wanted to do something like change the base Plate's color but to a random color every single time we join into the game or if we were to throw this inside of a loop or something like that we can actually do that by picking out random number values to put that as the color and so if we were to basically select the Bas plate over here and we selected this uh color over here so in the past we've worked with changing the brick color but there's actually another property that does the same thing which is color and this takes in three numbers to change the color rather than having a brick color object so if we select this then we can see this color wheel that basically allows us to change the color of something based on numbers itself so if we were to pick a random number between 0 and 255 for the red green and blue value then we can have a random color every single time so here's what we're going to do we're going to hit cancel and we're going to go to our random script we're going to delete this code that we have here so far and what we're first going to do is make reference to the base plate by saying local base plate equals game. workpace dobas plate just like this and then we're going to make a while loop by saying while true do and then hit enter so basically while true do this is what's called an infinite W Loop and generally I don't recommend using this because this is going to run throughout the entire duration of the game since this conditional is always going to be true but just for the sake of demonstration purposes this is what I'm going to be doing so when we're using math. random to pick a random color we need to have a red value a green value and a blue value and we want to pick a random number from 0 to 255 for each of these values so we're going to have three separate variables for each of them so we're going to say local Red value equals math.
random open close parentheses and then for the uh minimum we're going to put zero and then for the maximum we're going to put 255 now for the green value we're going to say local green value equals math. random and then in parentheses we're going to put zero and then 255 and then finally for the blue value like this we're going to set this equal to math. random and then 0 255 is the range so now that we have the red green and blue value let's change the Bas Plate's color this might not make sense to you so I want you to follow what I type here when we're changing the color value of the base plate so what we're going to do is say base plate.
color equals color 3. from RGB open and close parenthesis and then what we're going to put inside of here is the red value comma green value comma blue value just like this so this takes a color 3 object and we're passing in the three color values that that we created randomly with math. random and in order to see these changes what we're going to do is put in a weight statement and we need to make sure we do this otherwise this is going to execute very very quickly and it's going to crash our script so we need to put in a task.
weight for let's say about 2 seconds and now if we go into the game and hit play then what we should see is the base Plate's color changing every single time that 2 seconds have been passed so if we go into the workspace and then click on the base plate we can look down at the color property with a random red green and blue value that's being updated every 2 seconds and so this is how we use math. random effectively inside of our game now this is just a simple example but obviously you can do a lot more with math. random like let's say you were playing a matchmaking system and it was picking a random map for the players to play for the next match then that's what math.
random can be used for uh there's a lot of useful things you can do with it but this is just one simple example I'd like to throw out there for you to use okay now there are some things that we should know about math. random like the ranges itself so if I were to drop two lines down here and if I were to have another random value by saying local random number equals math. random open close parentheses it's actually possible to mess up the range if we have a minimum value that's larger than the maximum value so what I'm trying to get at here is if we have a maximum value of let's say 50 and if we were to have a minimum value of let's say negative 20 then this is not going to be a valid range because the minimum value has to be smaller than the maximum value and another thing is you can have negative values as a minimum value as well so if I were to basically comment out this code right here so I'm just going to quickly go up here comment out the code just like this by doing the double slashes and hard brackets and then having two end brackets by the end here if we were to try and print the random number now by going into the game and hitting play then it's going to say that invalid argument to number two to random and interval is empty the interval is empty because this is not a valid interval and we need to make sure that the minimum value is smaller than the maximum value when we're using math.
random so that's a little something I wanted to add in there when you're using math. random so that's basically going to be it for this episode for today's learning objective I want you to play around more with using math. random uh for your scripts whether that's changing the base plates color again to something else for a specific range uh or you could change other properties like transparency from 0 to to one and all these other sorts of things now there is one quick thing I forgot to mention and that is you can have math.
random being empty interval just like this and it's going to pick a random number between zero and one by default so it's going to pick a decimal number so if we go into the game and hit play then what we should see is a random decimal number inside of here that's being picked from 0 to one so that's another useful thing you can use if you're trying to create something random using map.