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
}
local actualTable = {}
local totalPercent = 0
local altPercent = 0
local chance = 0
local previousItem = 0
local drop = nil
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)
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.
9 comments
If you found this solution (technically just answer to request) helpful either directly or indirectly then please accept this answer and help others who're looking for the same answer.
Wow that's complicated. If you dont mind can you explain this a little bit more. Feel like im gonna need this later.
I mean it's really complicated but all it did is using 3 for loop with each have different purpose. It's basically add sum of previous to the value (inserted in table) and then compare the random value, when the random value is less than value from the table it then use value from table AND translate into text.
If the comment is complicated then you can see simplified explanation found in the post (I recently updated it)
Hey, this is kinda unrelated but what does the hashtag represent in actualTable[#actualTable]. Sorry if this seemed dumb because i'm still fairly new to coding.
#actualTable represent amount of item in table so actualTable has 4 items then #actualTable will become 4
so simply, drop on line 54 will result in a pet name?
Yeah. You can change the line 54 to utilize dataStore service (save pet to dataStore) or show player the pet with GUI
accept go brrrrrrrrrr