I have a string with over 2 million characters, and I feel like my current way of finding a random match from a pattern isn't fast as it could be.
local function getRandomMatch(string, pattern)
local occurenceCount = select(2, string.gsub(string, pattern, ""))
local index, randomIndex = 0, math.random(1, occurenceCount)
for match in string:gmatch(pattern) do
index = index + 1
if index == randomIndex then
return match
end
end
end
Is there a way this could be any faster?