How can math be used in usual scripting?

So I have thought that, I can use the math function for different tasks. Examples include:

math.random; used for randomizing values
math.round; used for getting approximate values of vars
math.abs; getting a positive answer for var (x)

I want to know how it can be used for common scripting use as it is an important part of programming knowledge.

4 Likes

Well, everything.

Pretty much everything in your game at some point requires calulations, and or basic arithmetic to function as a game.

The math library just provides you to just the basic tools for accomplishing this task, if you need a random number, ask Random.new() or math.random to give that number using a pseudo-random number generator, if you to round a number to appear more simplified, while still being approximate to a specific value, ceil floor and round are there, if you need to get the distance from 0, abs is there.

There isn’t a specific use for every program, its just a tool for when you need it.

3 Likes

math.random is very useful for adding variation even if it’s on little things such as cooldowns picking random values and etc
math.round just brings you to the closest interger which can avoid stuff like infinite punctuation
math.abs i don’t actually have an example for lol

Pretty nice, I see. Didn’t quite understand the math.round function tho

Are there more functions that I can use from math?

math.clamp(x, min, max)

Is what I find important to me. What it does if the number goes over the maximum amount it will return the max amount, and same thing with minimum. If it’s goes below the minimum then it will return the minimum count.

For example:

--math.clamp(number, minimum, maximum)
math.clamp(0, 1, 10) = 1
math.clamp(5, 1, 10) = 5
math.clamp(11, 1 ,10) = 10
1 Like

I have tried using math.clamp for a bit. For me, it’s similar to rounding a number to another variable.
What can it be used for? I tried to use math.clamp in the past but haven’t seen it’s point.

for example, you can have a custom damage/heal function, math.clamp would prevent hp going over the max limit,

hum.Health += math.clamp(20, 0, hum.MaxHealth) -- if player has 85 health it will only heal them for 15

I see. Does this like set the value if it’s higher?

Im not quite sure what you mean by that but uh, lets say you have 3 values,

local currenthp = 70
local maxhp = 100
local healamount = 250

you DON’T want currenthp value to be higher than max hp, so

-- math.clamp(x, minlimit, maxlimit)
currenthp += math.clamp(healamount, 0, maxhp)

the code above will set the currenthp to 100 because the healamount is 250 which is greater than the limit;
im really bad at explaining but i hope this clears it up for you

Yes. I meant exactly that. Like a limit for a value/variable.

How can I use the mod function? It’s not really much but it’s fractional division stuff.

That code will set currenthp to 170. Because it limits the addition to 100 instead of limiting currenthp to 100. This code limits currenthp to 100.

currenthp = math.clamp(currenthp + healamount, 0, maxhp)

I see. It looks like the demanded hp, the minimum hp and the maximum hp.

There is a good alternative to math.random called Random.new(). It’s a lot more flexible and supports floating point values (no more having to do math.random(10, 100) / 100).

math.abs at its core is actually really simple. If x is less than 0, invert it.
You can even make your own abs function if you really wanted too.

function abs(x)
    if x < 0 then
        return -x
    end
end

I don’t know much cases where you would always want a number to be positive, but its there if you need it.

Thanks for clarifying the Random.new. I’ll try that.

I have come across a command called randomseed and it’s pretty confusing to me. Is there an exact purpose for it?

randomseed sets the seed of the RNG. For Random.new(), the constructor takes in a seed parameter which does the same thing.

For a given seed, every output will be the same. Think Minecraft worlds. Seeds guarantee deterministic output for a RNG.

Trig functions like sin, cos, atan2, etc. Good for angle calculations and physics applications.

I see. Is there a difference between math.random & Random.new?

How are they used? I assume it’s some physics with exceptions. Being a ROBLOX player, I don’t know much about that.