Question on this code

local crate = {}
local rand = math.random(0,30)
if rand > 20 and rand < 23 then
crate.ID = “strangecrate”
elseif rand > 23 and rand < 26 then
crate.ID = “specialcrate”
elseif rand > 26 and rand < 29 then
crate.ID = “rarecrate”
elseif rand > 29 then
crate.ID = “unusualcrate”
else
crate.ID = “commoncrate”
end

I know that the lower the value, the higher the chance of getting that item.
But i have a problem, what if that part of the code is

" crate.ID = “unusualcrate”
else"

and doesnt have

“elseif rand > 29 then”

in it.

Look at it this way:

math.random( 1, 100 ); - let’s say 1-100 are valid. If you say >= 50 for one item, >= 1 && < 25 for another, and >= 25 && <50 for another. The item that has 50 numbers has a higher “chance” of being selected. Then, the last two have an equal chance with 24 per.

When you leave a number out, like that above, the else “common crate” will be selected at 23, 26, 29 and all numbers 20 and below; and decimals in-between if there are any.

If you have 21 or 22 you get special crate. 27 or 28 = “rare”, 30 = "unusualcrate

So as of now, im guessing the unusual is the rarest and common is the most common? Or am i wrong. If i raise the number for rare, those that make the unusual even more rare?

Assuming whole numbers only, and decimals: The unusual has the lowest chance with 1 chance, while the special and rate have an equal chance with 2 chances each, and the common will be the highest chance ( even if it only had the in-between values it would be, but it has 3 + 20 chances )

You can figure out percent by doing: ( ( number / total ) * 100 ):
1 = unusual = ~3.57 % chance
2 = rare = ~7.14 % chance
2 = special = ~7.14 % chance
23 = common = ~82.14 % chance

Added up for 99.99%, the ~ means approx.

Ahh okay thank you very much for clearing it out for me!