Probability System

hey! so i’ve been trying to make a chance system but none of the posts i’ve read helped me because everyone links that one weighted chance system post and i dont really understand it (oh and also it doesnt work for me)

so i came up with a solution, but theres a problem
it is very bad and i think it might cause lag

(here it is, prepare your eyes)

local rarities = {
	
	Common = 75,
	Uncommon = 20,
	Rare = 5
	
	
}

local function getRandom()
	local raritytable = {}
	for rarity,percentage in pairs(rarities) do
		for i = 1, percentage do
			table.insert(raritytable, rarity)
		end
	end
	print(raritytable[math.random(1, #raritytable)])
end

getRandom()

anyways if anyone could help it would be awesome, thanks!

2 Likes

I think you can better use the math.random function.
If you do a random between 1 and 20, and 1 is the rare one, you have a easy solution for your problem.
I know this is not the most efficient way, but should work.

1 Like

Change category to #help-and-feedback:scripting-support.

Have you seen mine? :pleading_face:

2 Likes

this looks like exactly what i need! does it support decimals?

It does! Just note that the chances must not add to over 100, or it will error. If they’re under 100, there’s a chance you could get nil back when generating a random item.

I plan to also remake the luck system because I’m not satisfied with how its made.

Here is a good Probability System plan and simple. (had to test it, sorry late post)

local function pickItem()
	local items = {
		Common = 75,
		Uncommon = 20,
		Rare = 10,
		Epic = 5,
		Legendary = 1
	}

	local roll = math.random(1, 100)
	local min = math.huge
	local pick

	for item, chance in pairs(items) do
		local diff = roll - chance
		if diff >= 0 and diff < min then
			min = diff
			pick = item
		end
	end

	return pick
end


print("Item:", pickItem())

This really is using a 1-100 percentage to item level.
(rounding downwards)

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