Reputation: 51
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?
Upvotes: 4
Views: 236
Reputation: 974
local find, random, match = string.find, math.random, string.match
local function getRandomMatch(string, pattern)
local pos, random_pos = 0, 0
for cnt = 1, math.huge do
pos = find(string, pattern, pos + 1)
if not pos then
return match(string, pattern, random_pos)
elseif random(cnt) == 1 then
random_pos = pos
end
end
end
for j = 1, 20 do
print(getRandomMatch("1234", "%d%d"))
end
UPDATE:
Fast-and-Dirty solution:
("Dirty" means "matches are random but chosen with non-equal probabilities")
local random, match = math.random, string.match
local function getRandomMatchFastAndDirty(string, pattern)
return match(string, pattern, random(#string)) or match(string, pattern)
end
Upvotes: 1