Hello, has anyone got any ideas on how I can create a lootbox system, with percentage of rarities. Thanks
Use the Roulette Wheel Selection Algorithm for that. In general, the roulette wheel selection method is a way to select items from a list with a probability that is proportional to their size or weight, and in your case, percentage.
To implement the roulette wheel selection method, you would first need to create a list of items and their corresponding percentages. Then, you would need to calculate the total weight of all the items in the list. Next, you would generate a random number between 0 and the total, and iterate over the list of items, summing the weights of each item as you go. When the sum of the weights exceeds the random number, the current item would be selected.
-- Create a list of items and their weights
local items = {
{name = "apple", weight = 10},
{name = "banana", weight = 20},
{name = "orange", weight = 30},
{name = "pear", weight = 40},
}
-- Calculate the total weight of all items
local totalWeight = 0
for i = 1, #items do
totalWeight = totalWeight + items[i].weight
end
-- Generate a random number between 0 and the total weight
local randomNum = math.random(0, totalWeight)
-- Iterate over the list of items and sum their weights
local sum = 0
local selectedItem
for i = 1, #items do
sum = sum + items[i].weight
if sum > randomNum then
selectedItem = items[i]
break
end
end
-- Print the selected item
print(selectedItem.name)
Damn that was fast. Works great, thanks
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.