So here what i mean:
Rat - 30% Dog - 25% Cat - 25% Golden Dog- 10%
How to make the system choose itself randomly with Chance based?
and Please explain to me and make it simple cause i only have 1 braincell left ;-;
Similar to:
Case Opening
Egg Hatching
Enemy Loot Drop
Thank's for Reading!
Write table that use all items and percentage. Then we add all previous percentages to the total percentage. The total percentage is used in adding the number to the maximum chance of each item (Example: Dog have the max number of 31 due to addition of 30 from Rat), the random function will then random the number and get the percentage which will be used to compare the chance of each item.
This means that each item will be rarer depend on the number of items found in the table (Due to the random algorithm being nature of unlimited percentage). This algorithm also meant that percentage can be unlimited (technically there's a number limit such as 64 bits OR 32 bits but that's irrelevant) and never maxed out at 100%.
==== Random Number based on Algorithm ====
1st Rat: 1-30
2nd Dog: 31-55 (+30 from total number)
3rd Cat: 56-80 (+55 from total number)
4th Golden Dog: 81-90 (+80 from total number)
NOTE: ITEM IN TABLE MAY BE DIFFERENT DUE TO ORDERING!!
==== Code ====
The code will use at least 7 variables, 2 variables are a table
to help in the for loop
of the function.
local dropTable = { Rat = 30, Dog = 25, Cat = 25, Golden_Dog = 10 } -- Table which fit item shown in the question post local actualTable = {} -- Table which list MAXIMUM chance local totalPercent = 0 -- Add number to max chance local altPercent = 0 -- Alternate version of totalPercent local chance = 0 -- Chance randomized by math.random local previousItem = 0 -- Chance of previous item to be compared local drop = nil -- Item which is gotten from randomization function addPercent() for i, v in pairs(dropTable) do wait() table.insert(actualTable, v + totalPercent) totalPercent = totalPercent + v end randomize() end function randomize() chance = math.random(1, actualTable[#actualTable]) for i, v in ipairs(actualTable) do wait() if i > 1 then if chance < previousItem then break end end previousItem = v end translateItem(previousItem) end function translateItem(GivenChance) for i, v in pairs(dropTable) do wait() if GivenChance == v + altPercent then drop = tostring(i) break end altPercent = altPercent + v end print(drop) -- Let's pretend that this is the item you got from randomization (opening egg) end addPercent()
==== SIMPLIFIED EXPLANATION ====
addPercent() function
Step 1: Value from the initial table (dropTable
) is added by the sum of the previous value (totalPercent
) and insert into another table (actualTable
). The number of each item is added in totalPercent
for each loop.
randomize() function (activated after step 1)
Step 2: Random value is then created AFTER step 1 and saved in chance
variable
Step 3: The first item (from actualTable
) is always skipped and the number of the first items is saved as previousItem
this is used to compare item which actually exists and avoid compare nil
Step 4: chance
is then used in for loop of randomize()
function. This will compare if chance
is less than the value of the previousItem
variable. If it failed then an item that has a higher value than chance
variable will be saved as the previousItem
variable.
Step 5: If it did found that chance is less than previousItem
then the previousItem
value is then used in translateItem()
function as parameter.
translateItem() function (activated after step 4)
Step 6: The parameter is then used in comparing values from dropTable
. Value from the initial table (dropTable
) is added by the sum of the previous value like Step 1 but utilizes AltPercent
instead of totalPercent
. (This step DON'T insert value into table)
Final Step: If the parameter matches the value from dropTable
plus AltPercent
then the string (Golden Dog, Cat, Dog, Rat) will be saved in drop. Drop is then printed.
==== CONCLUSION ====
This looks like a request question to me but I want to share my code which helps in the randomization of the game. (Simulator Games is an example) You can say that the code above is now open-source. It's not that hard to write anyway because anyone can figure it out.
Happy Scripting.
I kinda have an idea about how you can do it, but I'm not so sure if this is the right way, or if this is the smart way. To be honest this seems so basic, and you might want to wait for a better solution.
Okay, so basically you gotta use math.random. But i think this will only work on single items. Like, for example if you open an box, you either get something, or nothing. So basically, you do math.random for example it's between 1-100 so the chance is one out of a hundred.
--This runs when it's called, maybe you're using a Remote Event or anything. local chance = math.random(1,100) if chance == 1 then --1/100 print("Something1") elseif chance >=2 and <=89 then --what is it, idk, 88/100 chance? Wow my math is bad print("Something3") elseif chance >=90 then --10/100 print("Something2") end
This is like, the very basic way of doing it. If you're still new to coding and don't understand much.