U
Size: a a a
U
U
z
RS
G
local function rand_insert(str, count, ch)
return table.concat(setmetatable({i = 1}, {
__len = function() return #str + count end,
__index = function(t, idx)
if math.random() < count / (#str + count) then
return ch
else
local c = str:sub(t.i, t.i)
t.i = t.i + 1
return c == '' and ch or c
end
end
}))
end
local result = rand_insert('aaaaaaaaaaaaa', 6, '.')
CP
RS
aaaaaaaaaaaaa -> .aaaaaa.aa.a..a..aaa
?RS
RS
RS
RS
local input = "aaaaaaaaaaaaa"
local input_length = string.len(input)
local min_dots = 1
local max_dots = input_length - 1 -- just for convenience
math.randomseed(os.clock() * 1000000)
local number_of_dots = math.random(min_dots, max_dots)
local output_table = {}
for character in string.gmatch(input, ".") do table.insert(output_table, character) end
for i = 1, number_of_dots do table.insert(output_table, ".") end
for i = #output_table, 2, -1 do
local j = math.random(i)
output_table[i], output_table[j] = output_table[j], output_table[i]
end
local output = table.concat(output_table)
pprint(input .. " -> " .. output)
S
function trashify(str)
for i = 0, math.random(#str) do
local slice = math.random(#str)
local left, right = str:sub(0, slice - 1), str:sub(slice)
str = left .. "." .. right
end
return str
end
S