Introducing MathAddons!

Hey I was bored so I decided to make this for you:

local module = {}
module.random = function (decimal, array)
	print(decimal)
	local min = array[1]
	local max = array[2]
	local finalnum = 0
	local finalnum = finalnum + math.random(min, max)
	if finalnum ~= max then
		for i=1, decimal, 1 do
			finalnum = finalnum + (math.random(0,10)/10^i)
			print(i)
			print(finalnum)
		end
	end
	return finalnum
end
return module

to call you just do what you asked for

hope you like it

local module = {}

module.random = function(Decimals, Table)
    local Number = Random.new():NextNumber(Table[1], Table[2])
    local NOD = 1 / (10^Decimals)
    return math.floor(Number / NOD) * NOD
end

return module

this is much shorter then what you made

1 Like

thanks, I guess im not the best with math programming but I thought it would be fun to make it

Thats a pretty cool idea to add!
Would you mind me adding it to the main module (Iā€™ll rewrite it of course)

oh yea sure, I donā€™t mind that at all

2 Likes

I recommend if you genuinely use this require the id instead so the module updates automatically

local ms = require(7066695577)
print(ms.round(1.48723,2))

Added a random function
What it does (if you havent read in the posts below) it has a second arguement for rounding to decimal places
Heres the code just to prove i rewriten it

function MS.random(x,d) -- math.random but the second parameter is the decimal place (optional)
	if type(x) == "table" then
		if d == nil then
			d = 0
		end
		local r = 10^d
		return math.random(tonumber(x[1])*r,tonumber(x[2])*r)/r
	else
		return nil
	end
end

More info in the original post

Iā€™ve also added an average function

function MS.avg(x) -- gets the average of a table
	local sum = 0
	for _,v in pairs(x) do 
		sum = sum + v
	end
	return sum / #x
end

More info on that as well in the original post!

function MS.random(x, d) -- math.random but the second parameter is the decimal place (optional)
    if type(x) ~= "table" then
        return nil
    end
    d = d == nil and 0 or d
    local r = 10^d
    return math.random(tonumber(x[1]) * r, tonumber(x[2]) * r) / r
end

this can be shortened to

sum += v

hope this reply helps

2 Likes

I know that x -= y or x+= y exists. And my code can be shortened, i just find it easier to read.

1 Like

for average function you can use ... instead of table as parameter

function avg (...)
sum = 0 
for i,v in pairs({...}) do
sum += v
end
return sum / #{...}
end


print(avg(1,5,6,23,45,76,5,232,5,76,3,6,45,23,4,45,23,5)) -- giving arguments instead of table of numbers

That does look easier for readability! But Iā€™ll use a practical use for an example, you have a table of all the player stats in the game. And you want the average. I dont think you can use your way in that case. Other than practical uses I think that would be a better idea

2 Likes

Added a new functionā€¦ Time!

This returns the time of a float value from 0-24, for example 15.5 would be 15:30 or 3:30 PM. Seconds support coming soon

local ms = require(game.ReplicatedStorage.MathService)
print(ms.time(13.25)) -- using 1 parameter would give you the 24 hour clock, this'll return "13:15"
print(ms.time(13.25,true)) -- the second parameter is whether or not you want to use the 24 hour system or 12 hour. This'll return "1:15 PM"
1 Like

Is it okay if I create a math resource?

Elaborate on what a ā€œmath resourceā€ is.

Oh, basically Iā€™m creating another math module, is it fine with you?

Sure why not! If its using some of the code from this module i wouldnt mind some credit.

Donā€™t worry since Iā€™ll make up my own code! Furthermore, I asked this so people wouldnā€™t say, ā€˜There are a lot of other resources out thereā€™.

2 Likes

Then permission to make another module wasnt even necessary, as long as you can come up with your own stuff it wont disrupt me.

2 Likes

I looked at this and was surprised, math modules are not copyrighted
you can make whatever :joy:

3 Likes

Now that I think about it, I should trademark my module :thinking:
Any unauthorized use of ā€œSquiegysā€™ MathServiceā€ will result in fines up to 5,000 robux!! :rage:

3 Likes

Requirements for using my trademark is adding me to team create on every single project youā€™re working on :smiling_imp:

2 Likes