Problems with picking items randomly by chance

this is what i got right now:

	if math.random(0,100) <= weight then
		return true
	end
	return false

and i have a item thats like 0.0000001% as jokes and alot of the players that played my game got it pretty fast, not sure if it was luck or the system

1 Like

Could you provide an example weight value and expected %?

2 Likes

Whats the weight? Is this the code for the rare item? If so, then if you are doing something like 0.0000001 as the weight, the odds wont actually be 0.0000001%, it will be 0.99%. This is because math.random(0,100) only picks whole numbers. In other words, the item will be given if 0 is selected out of the 101 possible whole numbers ranging from 0 to 100. What you may be looking for instead is:

local weight = 0.0000000001 -- this is equivalent to 0.0000001%, see below comment
	if math.random() <= weight then -- math.random() without arguments returns any decimal number between 0 and 1
		return true
	end
	return false
1 Like

what do you mean?
not much to provide you

1 Like

When you declare a range for the math.random function. It returns an integer value from the specified range. If you just use math.random with no parameters it should return a float value.
So try this simple change first before anything more advanced can occur. Additionally, you should use the print for checking if the random function is actually returning float values.

(As I am writing this, I was not able to completely test this through roblox studio.)

local weight = 0.0000000001

if math.random() <= weight then
    return true
end

return false

If you want to do it using integers you could use this structure of code.

-- You can use floats to define these variables.
local minPercentage = 1   -- Minimum percentage value (e.g., 1%)
local maxPercentage = 100 -- Maximum percentage value (e.g., 100%)

local randomPercentage = math.random(minPercentage, maxPercentage)
print(randomPercentage .. "%") -- Print the random percentage

We need the weight being used. Also, check Template_Xx and my answer as well.