How do i make something randomly happens with a percent rate?

Im new to script and i want to learn, im not asking a full script. Any suggestions will help me alot thanks

1 Like

I know you didn’t ask for a full script but I don’t know how to explain it without one

local percentage = 75
if math.random(100) < percentage then -- ~75% chance of random number being lower than 75
    -- make something happen here

Math.random isn’t reliable for percentages, since it’s random; Not mathematically calculated. If you had a 10% chance at a legendary and 90% chance at a common, you could end up getting all legendaries and no commons, because it’s randomized.

As for @MessJessGaming, refer to this post: Pick random item based on percentages - #3 by goldenstein64


Yeah if there is a 10% of getting legendaries that means that there is a small chance to get all legendaries.

Also the reference you posted uses, which uses the same algorithm as math.random()


I do get that, but it doesn’t just rely on that.

Also, randomizing something doesn’t decrease the chance of getting a number. It’s the same chance to get any number, but it randomizes that number. Using a mathematical equation to determine rarities is way more reliable.


Your reference does the same thing, just for multiple outcomes

-- creates a random number generator, (math.random already has one created)
local R =

-- in `math.random(100)` this is used
local MAX_RANGE = 100
-- min is omitted because in `math.random` 0 is the default min
local MIN_RANGE = 0

local percentages = { -- list of random things that can happen
    -- when you want something to happen or not you can just use a single percentage
    -- (`local percentage` is that single percentage)
    Common = {50, 100},
    Uncommon = {25, 50},
    Rare = {10, 25},
    Epic = {0, 10}

-- identical to `math.random(MIN_RANGE, MAX_RANGE)`
-- or in our specific case `math.random(100)`
local getRandomItem = R:NextNumber(MIN_RANGE, MAX_RANGE)

local found
-- loop because there are multiple percentages
for item, bounds in pairs(percentages) do

    -- `getRandomItem < bounds[2]` is the same as `math.random(100) < percentage`
    -- `getRandomItem >= bounds[1]` is omitted because we already know that the random number is above 0
    if getRandomItem >= bounds[1] and getRandomItem < bounds[2] then

        -- make something happen here

        found = item
1 Like