So I have a table with percentages 25 50 75 etc when I index that table I want to choose one based on that percentage if C has a percentage of 50 then I want it to choose C based on a 50/50 same if B has a percentage of 25 I want it to pick B %25 of the time.
I know how to calculate percentage but I do not know how to use it.
Here is my code with the table and how I calculate percentage but I don’t think calculate percentage is relevant here since I want a picker not a percentage applier:
local Table = {
A = 0,
B = 25,
C = 50,
D = 75,
E = 100 --I know it will choose E every time this is just an example.
}
--This is how to calculate percentages but I don't think I use it here.
local Number = 100 --Number to apply percentage.
local Percentage = .25 --%25
Number = Number * Percentage
If D has a %75 percentage chance then it should be picked more than C how do I do this?
Thanks.
P.S: Please try and give a simple answer try not to overcomplicate it but feel free to explain how it works and can be improved. Also make sure it works! Also I have looked and the other threads was not what I was looking for.
I highly doubt a couple of tables is a huge performance problem. If you think about how percentages work, I believe this is the only way to do this anyway.
local Number = 100
local Percentage = 50
local RandomNew = Random.new()
local NextInteger = RandomNew:NextInteger(0, Number)
if NextInteger < Percentage then
print("Selected! Chance: %" .. Percentage .. ".")
end
Example Usage:
local HttpService = game:GetService("HttpService")
local Message = Instance.new("Message")
Message.Name = HttpService:GenerateGUID(true)
Message.Parent = workspace
local Number = 100
local Percentage = 50
local RandomNew = Random.new()
local Attempt = 0
local Selected = 0
for Number2 = 1, 100 do
local NextInteger = RandomNew:NextInteger(0, Number)
if NextInteger < Percentage then
Selected = Selected + 1
end
Attempt = Attempt + 1
end
Message.Text = "Attempt: " .. Attempt .. "\n" .. "Selected: " .. Selected .. "\n"