Getting a percent chance based on weight

Hey!

I have a weighted chance system. The module looks like this:

local module = {}

module.Chances = {
	
	["Naruto Pack"] = {
		
		["Naruto S1"] = 0.3,
		["Sasuke S1"] = 0.3,
		["Sakura S1"] = 0.3,
		["Kakashi S1"] = 0.3,
		["Gaara S1"] = 0.3
		
	},
	
	["One Piece Pack"] = {

		["Luffy S1"] = 0.3,
		["Zoro S1"] = 0.3,
		["Nami S1"] = 0.3,
		["Robin S1"] = 0.3,
		["Sanji S1"] = 0.3

	},
	
	["Dragon Ball Pack"] = {

		["Goku"] = 0.3,
		["Gohan S1"] = 0.3,
		["Krillin S1"] = 0.3,
		["Piccolo S1"] = 0.3,
		["Vegeta S1"] = 0.3

	}
	
}

return module

The script looks like this:

local function GetCard(pack)
	
	local ChanceTable = {}
	
	if PacksModule.Chances[pack] then
		ChanceTable = PacksModule.Chances[pack]
	else
		return
	end
	
	print(ChanceTable)

	local Weight = 0

	for _, Chance in pairs(ChanceTable) do
		Weight += (Chance * 10)
	end

	local ranNumber = math.random(1, Weight)

	Weight = 0
	for prize, chance in pairs(ChanceTable) do

		Weight += (chance * 10)

		if Weight >= ranNumber then
			return prize
		end

	end
	
end

I was wondering how I could turn the chances from the module into percentage to display to the user

You could just multiply the chance with 100 and use it as the numerator.

local chance = 0.3
local numerator = chance * 100 --30
local percentage = `{numerator}/100`

print(percentage) -- 30/100
1 Like

Hey. It’s not a 30% chance though because if you add them all up it equals 150/100. Thanks for the help though

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