How can math be used in usual scripting?

Yes. math.random uses the old RNG. Random.new uses a more sophisticated one (which is also more random). Use Random.new. math.random is deprecated (not supported for future use).

As for your other question, yes, mostly for physics applications, and for things like working with angles (dot product, cross product, etc.) There are tons of trig applications, and you will have to simply look them up.

1 Like

When I do something like Random.new(3,4) (correct my scripting please) then it just prints “Random” and nothing else.

The Random.new(seed) method creates a new Random object. Think of it like calling a constructor of class Car. Calling c = Car(1965, "Chevy", "Impala") just gives you a Car object stored to c. To use it, you need to use the methods in c, like c:Drive().

Same concept here – you’re just printing the Random object instead of using it to generate a random number. You need to store the Random object as a variable and call the NextInteger method.

Rnd = Random.new(1) -- Leave constructor blank for random seed
print(Rnd:NextInteger(3, 4))

You can also use NextNumber to generate a random float.

1 Like

The math library has quite a few methods in it. You can find it here:

Now with that, I find that I use math.rad, math.deg, and math.floor a lot. Occasionally, I use the trig functions of math.sin and math.cos when working with angles.

There seems to be some confusion as to what math.clamp is used for. What it does it clamps a value between two limits which are a minimum value and a maximum value. The result is guaranteed to be within that range, inclusive.

As for math.random, that one uses what’s known as a Linear Congruential Generator to generate random numbers, but they are not really random. It always generates the same sequence of numbers no matter what seed is used. All the seed does is pick the starting point of the list to start generating numbers at. There’s a pretty good writeup on Wikipedia about it

math.abs or absolute value is what I like to call the magnitude function. If you have a situation where you cannot have a negative number, this is the one to use. Things that it can be used for is distance. When you take the magnitude of a vector, the result is always positive.

1 Like

I liked your structured answer. Are the rad, deg and floor do a similar purpose? For me deg and especially other functions seems like some replacement for Vector3 when using rotations, or some radius math with circles to determine some rotation of an object,

No, they do not.

math.deg and math.rad are inverses of each other. math.deg converts an angle from radians into degrees while math.rad converts an angle from degrees into radians. The trigonometric functions of math.sin, math.cos, and math.tan all take angles in radians. You will need to take a class in trigonometry to really understand those trig functions, but basically it involves the unit circle (a circle with a radius of 1), and the ratios of the lengths of the sides of a right triangle. When dealing with trigonometry, π becomes very important because 2π radians = 360° which is a full circle. A radian is defined as the radius of a circle laid down along the circumference. The math library has a math.pi property that you can use that’s literally equal to 3.141592653589793.

If you look at the math library that Roblox provides, you will also see math.sinh, math.cosh, and math.tanh. Those are the hyperbolic trigonometric functions. Instead of being based on a right triangle/circle like the normal trig functions (Which are ultimately based in Euclidean geometry which everyone is familiar with.), these are based on geometric shape known as a hyperbola which is a type of conic section (Hyperbolic Geometry). You can use them like the regular trig functions, but you get way different numbers. And just so you know, there are other types of geometries out there besides these two, each with their own rules and math.

math.round, math.floor, and math.ceil are related to each other and are used to remove the fractional component of a floating point number turning it into an integer. math.round rounds the number to the closest integer. In the case of 0.5, it rounds it away from zero. What that means is that +0.5 would be rounded to 1 while -0.5 would be rounded to -1. math.floor returns the largest integer that is smaller than or equal to the number you give it. For instance, math.floor(2.324) will give 2 as a result, but math.floor(-2.324) will give -3. math.ceil is the opposite. It returns the smallest integer that is larger or equal to the number you give it. So math.ceil(2.324) returns 3 while math.ceil(-2.324) returns -2.

There are various exponential and logarithmic functions as well, but the don’t really come up unless you’re working with specific types of data like signal processing or scientific data. Decibels, which is a measure of signal strength, is a logarithmic scale. I have rarely used e^x or ln(x) in actual practice outside of data analysis and calculus stuff. However, I have used log2(x) in practice with is a logarithm for base 2. That does get used…sometimes.

This post represents several semesters of mathematics at the college level.

4 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.