Mean, Median, Mode, Range, Minimum, Maximum, Sum formulas/functions

Heya, I was kind of bored, and wanted to make something and improve my skills a little bit more with math, so I made these functions for people to use if they have no idea how to actually make one themselves.

Median:

function CalculateMedian(NumberSet: {number}) : number
	if NumberSet == nil or #NumberSet == 0 then
		warn("No data provided.")
		return
	end
	
	for i,v in ipairs(NumberSet) do
		if typeof(v) ~= "number" then
			warn("NaN: "..type(v))
			print("Invalid type material: "..tostring(v)..", index: "..i)
			return
		end
	end
	
	table.sort(NumberSet, function(a, b)
		return a < b
	end)

	local index : number = math.floor(#NumberSet / 2) + 1

	if #NumberSet % 2 == 0 then
		return (NumberSet[index - 1] + NumberSet[index]) / 2
	else
		return NumberSet[index]
	end
end

Some uses for the median:

  • Balancing gameplay: Help improve gameplay by calculating a fair amount of health for enemies/players, and setting fair rewards.

  • Fair Matchmaking: Can calculate more fair teams for matchmaking/queue in competitive matchmaking.

Why use median instead of mean?
Unlike mean, the median is less affected by outliers/extreme values, making it more useful in some situations

Formula: Sort the numbers lowest to highest or vice versa, and get the middle number of the list.

next up…

Mean/Average:

function CalculateMean(NumberSet : {number}) : number
	if NumberSet == nil	or #NumberSet == 0 then
		warn("No data provided.")
	end
	
	for i,v in ipairs(NumberSet) do
		if typeof(v) ~= "number" then
			warn("NaN: "..type(v))
			print("Invalid type material: "..tostring(v)..", index: "..i)
			return
		end
	end
	
	local TotalValue : number = 0
	
	for _,v in pairs(NumberSet) do
		TotalValue += v
	end
	
	return TotalValue / #NumberSet
end

Some uses for the Mean/Average:

  • Average Scores: Calculate statistics like K/D ratio, or adjust rewards to this benchmark.

  • Dynamic difficulty adjustment: Adjust difficulty based on the players stats dynamically in game.

Why use mean instead of median?
Mean is useful when you want to account all values in a data set, and overall trends, but not ideal with outliers/extreme numbers.

Formula: Add up all numbers in the data set, and divide by the amount of numbers in the data set

next…

Mode:

function CalculateMode(NumberSet: {number}) : number
	if NumberSet == nil or #NumberSet == 0 then
		warn("No data provided.")
		return nil
	end

	for i, v in ipairs(NumberSet) do
		if typeof(v) ~= "number" then
			warn("NaN: " .. typeof(v))
			print("Invalid type material: " .. tostring(v) .. ", index: " .. i)
			return nil
		end
	end

	local frequency = {}
	
	for i, v in ipairs(NumberSet) do
		frequency[v] = (frequency[v] or 0) + 1
	end

	local firstFrequency = frequency[NumberSet[1]]
	local allSameFrequency = true

	for i, v in pairs(frequency) do
		if v ~= firstFrequency then
			allSameFrequency = false
			break
		end
	end

	if allSameFrequency then
		warn("No mode.")
		return
	end

	local maxFrequency = 0
	local mode = nil
	local modes = {}

	for i, v in pairs(frequency) do
		if v > maxFrequency then
			maxFrequency = v
			mode = i
			modes = {i}
		elseif v == maxFrequency then
			table.insert(modes, i)
		end
	end

	
	if #modes > 1 then
		warn("No unique mode.")
		return
	end

	return mode
end

Some uses for mode:

  • Map selection: Get the highest vote in the map pool, can be used to choose between multiple maps.

idk any more oops…

Mode can be used for calculate what resources are used the most.

Formula: Get the most frequent number in the data set.

uhhh, what’s next…oh right…

Range:

function CalculateRange(NumberSet: {number}) : number
	
	if NumberSet == nil or #NumberSet == 0 then
		warn("No data provided.")
		return nil
	end

	for i, v in ipairs(NumberSet) do
		if typeof(v) ~= "number" then
			warn("NaN: " .. typeof(v))
			print("Invalid type material: " .. tostring(v) .. ", index: " .. i)
			return nil
		end
	end
	
	table.sort(NumberSet, function(a,b)
		return a < b
	end)
	
	local SmallestNumber = NumberSet[1]
	local LargestNumber = NumberSet[#NumberSet]
	
	return LargestNumber - SmallestNumber
end
  • Leaderboard Design: Can be used to group people in certain skill groups, e.g. bronze, silver, gold. For fair matchmaking or e.t.c.

idk any more, maybe you can come up with more lol…

Formula: Subtract the smallest value from the largest value in the data set.

The rest is self explanatory.

Minimum:

function CalculateMinimum(NumberSet: {number}) : number
	if NumberSet == nil or #NumberSet == 0 then
		warn("No data provided.")
		return nil
	end
	
	for i, v in ipairs(NumberSet) do
		if typeof(v) ~= "number" then
			warn("NaN: " .. typeof(v))
			print("Invalid type material: " .. tostring(v) .. ", index: " .. i)
			return nil
		end
	end
	
	table.sort(NumberSet, function(a, b)
		return a < b
	end)
	
	return NumberSet[1]
end

Gets the smallest number in a data set.

Maximum:

function CalculateMaximum(NumberSet: {number}) : number
	if NumberSet == nil or #NumberSet == 0 then
		warn("No data provided.")
		return nil
	end

	for i, v in ipairs(NumberSet) do
		if typeof(v) ~= "number" then
			warn("NaN: " .. typeof(v))
			print("Invalid type material: " .. tostring(v) .. ", index: " .. i)
			return nil
		end
	end

	table.sort(NumberSet, function(a, b)
		return a < b
	end)

	return NumberSet[#NumberSet]
end

Gets the biggest value in a data set.

Sum:

function CalculateSum(NumberSet: {number}) : number
	if NumberSet == nil or #NumberSet == 0 then
		warn("No data provided.")
		return nil
	end
	

	for i, v in ipairs(NumberSet) do
		if typeof(v) ~= "number" then
			warn("NaN: " .. typeof(v))
			print("Invalid type material: " .. tostring(v) .. ", index: " .. i)
			return nil
		end
	end
	
	local Sum : number = 0
	
	for _,v in ipairs(NumberSet) do
		Sum += v
	end
	
	return Sum
end

Gets the sum/total of the data set.

Let me know if this was useful(it probably isn’t because these are mainly used for analytics) and if I should make more of these resources. Anyway, that’s basically it for today.

Thank you.

-JAcoboiskaka1121

5 Likes

this seems very nice. I could see myself using many of these.

you should put these in a module.