I have alot of Ideas for math.random. My problem is it is very confusing to me! Here is an example.
Lets Say I wanted my script to choose a random map.
Maps = game.ServerStorage.Maps:GetChildren() Maps.math.random
--This is where I get stuck!
If possible, could someone show me an example on how it works? Thank you!
The math.random function returns random numbers based on the given arguments. It can work in 3 ways:
No arguments:
print(math.random()) -- No arguments, and returns a random number between 0 and 1 (0.1,0.2,0.3, ect)
One argument:
print(math.random(5)) -- Prints a random number between 1 and 5
Two arguments
print(math.random(5,10)) -- Prints a random number between 5 and 10
This algorithm can be used in a lot of useful ways, especially when creating a game based off random events. But, this function only returns a numerical value. Which means you have to base your random events off the numerical value it returns.
I'll give an example in the form of your scenario:
local Maps = game:GetService("ServerStorage").Maps:GetChildren() local RandomMap = Maps[math.random(#Maps)] -- "#Maps" is the numerical representation of how many maps there are. -- So we can get a random number between 1, and #Maps
GetChildren() returns a table of children from the parent it's called on. You can index this table with a number. In this case, i indexed it with a random number. Giving me a random key (map).
Edit:
And yes, you can use this with various types of Colors, such as creating a random BrickColor, or creating a random Color3 color (for Guis, Effects, and anything that has a Color or Color3 property). Though these methods don't involve using the "random" function from the "math" table.
Here are some examples:
Using BrickColor.Random():
local p = Instance.new("Part",workspace) local RandomBrickColor = BrickColor.Random() -- Random brick color p.BrickColor = RandomBrickColor
You can also convert this random brick color to a Color3 value, so it can apply to any object that has a Color3 property:
-- Assuming this script is inside a GUI with a color value local RandomColor3 = BrickColor.Random().Color -- Using .Color will convert it to Color3 script.Parent.BackgroundColor3 = RandomColor3
Though, most people tend to stick to the method of calling math.random 3 times in place of the RGB values of Color3 like this:
-- Assuming this script is inside a GUI with a color value local RandomColor3 = Color3.new(math.random(), math.random(), math.random()) script.Parent.BackgroundColor3 = RandomColor3 -- Since Color3's RGB arguments are based on a scale of 0 to 1, this method will return a random Color3 value.
But either way works, so it all depends on what you like best. Hope this helped.