How could I implement this randomizer logic?

Hey, so I’m working on a pet system. And for this system, the following pet chances are used:

  • 30% of getting Pet1
  • 30% of getting Pet2
  • 30% of getting Pet3
  • 10% of getting Pet4

Usually what I would do for this, is iterate over each pet in the chance table, starting with the lowest chance and ending with the highest chance, and for each one, generate a new random number between 1-100.

However, this wouldn’t work in this scenario, as there’s 3 pets all with the same chance…

I find this a bit confusing. Any ideas? Thanks :slight_smile:

Here is something I did for my pet system and seems to be working pretty fine.

local function ChosenPet()
    local CurrentChance = math.random(0,100)
    if CurrentChance >= 1 and CurrentChance <= 30 then
        return pet
    elseif CurrentChance >= 31 and CurrentChance <= 60 then
       return pet
    elseif CurrentChance >= 61 and CurrentChance <= 90 then
        return pet
    elseif CurrentChance >= 91 and CurrentChance <= 100 then
        return pet
    end
end
1 Like