Help with weighted math

I was using this tutorial
Heres my script to start

local prizeTable = {
	
	['Kamado'] = 0.1,
	['Tsugikuni'] = 0.1,
	
	
	['Lame Kamado'] = 0.2,
	
	
	['Uzui'] = 0.4,
	['Rengoku'] = 0.4,
	
	
	['Kanroji'] = 0.5,
	['Tomioka'] = 0.5,
	['Iguro'] = 0.5,
	['Shinazugawa'] = 0.5,
	['Himejima'] = 0.5,
	['Tokito'] = 0.5,
	['Kocho'] = 0.5,

	
	['Urokodaki'] = 0.6,
	['Kuwajima'] = 0.6,
	
	['lame'] = 0.8,
	
	
}
local Weight = 0
local ranNumber = math.random(1, Weight)


Weight = 0
for _, Chance in pairs(prizeTable) do
	Weight +=(Chance * 10)
	if Weight >= ranNumber then
		print('Congrats you won '..Prize)
		break
	end
	
end

I get a error on line 31

local ranNumber = math.random(1, Weight)

I have no clue what is wrong with it though

1 Like

Your Weight should not be 0 or lower than 1. If you try math.random(1, 0) it will give you an error.

1 Like

Thanks but one more question, how would I detect what it actually got?
The line print('Congrats you won '..Prize) didn’t work.

Where did the “Prize” come from.

no clue tbh
in the tutorial i used it just came out of nowhere ;-;

Not sure at all but just assuming, instead of underscore in the in-pair loop, Prize would be the identifier for the key.
Like this -

for Prize, Chance in pairs(prizeTable) do
	Weight +=(Chance * 10)
	if Weight >= ranNumber then
		print('Congrats you won '..Prize)
		break
	end
end

This isn’t related to the error shown in the console in any way though.
The error is because,
math.random(m,n)
if (n < m) then it will throw the error. Like in your case.

1 Like