% under 1 issue

So I used a script from a yt video and changed it up a bit and changed some of the percentages but any percentage under 1 will not be chosen

Script:

image

Percentages module:

image

That’s because math.random() cannot generate numbers that have a decimal number, the solution is to use (math.random(1000)/10), so you’d have to completely change your percentage module.

What would I change the percent module to so it could support whole numbers and percentages under 1%

You can sum up the chances, then multiply the sum by 10, then use that sum to generate a random number, then devide that multiplied number by 10.

Like this:

local SwordName,Chance in pairs(loottable) do
      Sum += Chance
end
local RandomNumber = math.random(Sum*10)/10

Why would I multiply and divide it by 10

Let’s say you have this loot table:

return {
      ["Common"] = 50,
      ["something"] = 50,
      ["rare"] = 0.1
}

The sum would be 100.1 when we sum up the chances, but you cannot use math.random() on a number with decimals so we multiply it by 10 and then devide it by 10 to return the random number.

The number that you’d multiply and devide varies based on how many digits is after the decimal.

So if I have multiple percents such as: 0.1,0.01,0.001,0.0001,0.05

What do I multiply and divide it by

For 0.1 you’re gonna multiply and devide by 10, and 100 for the 0.01, and 1000 for the 0.001, and 10000 for 0.0001, and 20 for 0.05 and so on.

Just use Random.new() and :NextNumber/:NextInteger
It works for decimals.
Example

local random = Random.new()
print(random:NextNumber(-.5,.5)) -- this prints a number in between -.5 and .5 

Ill leave it up to you to implement it into your code.

Also im pretty sure thats not a percentage system, its a weighted random.

Well then how would I create a percentage system because I once tried a system where it inserts 50 values with the percent 50 into a table but that’s not that reliable because I cannot enter values under 1%

So I need to have a if statement for each percentage less than 1%

A weighted random system is probably good enough for your case. I am not exactly sure how you would implement a percent system.

But how would I have multiple values under 1% for my system cause I cannot have multiple if statements