[Dev Resource] RandomWeight - Item Probability Picker

What is this?

This is a dev resource, this means it won’t “work” as you think on its own. This “script” is composed of two main functions that can be used to pick a “random” item from a list. However, the “random” pick is based on probability. So basically a RNG function.

How can this be used?

There are many ways you can use this in your scripts to make them shorter and more efficient. You can use this instead of making messy-looking elseif statements to calculate probability.

Example of messy-looking code (sometimes less efficient too):

local number = math.random(1,100)
if number > 90 then
	-- 9% probability this will happen
elseif number > 70 then
	-- 19% probability this will happen
elseif number > 20 then
	-- 49% probability this will happen
end 

Instead, with this “script” you can just set a table up and call a function to format it. Then it is good to be used.

How do I implement this into my scripts?

There are 2 main functions of this “script” a formater and a getter both of witch have been exported on the client and server side (if you use the “script” version instead of just implementing it directly).

local myTable= {
	[1] = { ['name'] = 'Item1', ['probability'] = 0.6 }, -- 0.6%
	[2] = { ['name'] = 'Item2', ['probability'] = 1.5 }, -- 1.5%
	[3] = { ['name'] = 'Item3', ['probability'] = 3.4 }, -- 3.4%
	[4] = { ['name'] = 'Item4', ['probability'] = 6.3 }, -- 6.3%
	[5] = { ['name'] = 'Item5', ['probability'] = 6.6 }, -- 6.6%
	[6] = { ['name'] = 'Item6', ['probability'] = 8.5 }, -- 8.5 %
	[7] = { ['name'] = 'Item7', ['probability'] = 35.7 }, -- 35.7%
	[8] = { ['name'] = 'Item8', ['probability'] = 37.4 }, -- 37.4%
}
local EvaluatedTable = EvaluateTablePropability(myTable)
local returnedValue = GetRandomWeightedItem(EvaluatedTable, 1)
-- returned value is the ["name"]

Example

local BloodTypes = {
	[1] = { ['name'] = 'AB-', ['probability'] = 0.6 }, -- 0.6%
	[2] = { ['name'] = 'B-', ['probability'] = 1.5 }, -- 1.5%
	[3] = { ['name'] = 'AB+', ['probability'] = 3.4 }, -- 3.4%
	[4] = { ['name'] = 'A-', ['probability'] = 6.3 }, -- 6.3%
	[5] = { ['name'] = 'O-', ['probability'] = 6.6 }, -- 6.6%
	[6] = { ['name'] = 'B+', ['probability'] = 8.5 }, -- 8.5 %
	[7] = { ['name'] = 'A+', ['probability'] = 35.7 }, -- 35.7%
	[8] = { ['name'] = 'O+', ['probability'] = 37.4 }, -- 37.4%
}
local EvaluatedTable = exports['RandomWeight']:EvaluateTablePropability(BloodTypes)
local count = {
	['AB-'] = 0,
	['B-'] = 0,
	['AB+'] = 0,
	['A-'] = 0,
	['O-'] = 0,
	['B+'] = 0,
	['A+'] = 0,
	['O+'] = 0,
}
for i=1, 1000, 1 do
	local bloodType = GetRandomWeightedItem(EvaluatedTable, 1)
	if count[bloodType ] then
		count[bloodType] = count[res] + 1
	end
end
for i,k in pairs(count) do
	print(i, k)
end

This is a very basic example (percentages were from this article).

Download
Download v1.0 from my Github Click Here

Disclaimer
This is not the best way to make this function nor the most efficient. I made this at like 5 am and did very little testing. This could break at higher loads. Additionally, this is a dev resource so I won’t be giving much support on this thread if you do have a question leave it in the comments and maybe someone can answer it for you if I am not available.

4 Likes

How can I use it?

Just are functions, make your brain work to understand them

EDIT: btw, nice work

1 Like

Cool release for people who don’t knowhow to do this i guess :slight_smile:

Nice work as always.