I used this str_shuffle() function in PHP and meka this api I need to do the same idea shuffle the letters With space between them but with lua to working with telegram bots I searched a lot and found nothing similar
Asked
Active
Viewed 1,472 times
2
-
1You could implement [this](https://stackoverflow.com/questions/3943772/how-do-i-shuffle-the-characters-in-a-string-in-javascript) in lua. I am going to try it, however, I don't know lua so it might take a little while. – Raphael Facredyn Aug 08 '18 at 17:40
-
Thanks man, waiting for you – Skora Aug 08 '18 at 18:30
-
I'm working on it, but it's hard and I had to do something first – Raphael Facredyn Aug 08 '18 at 18:52
3 Answers
2
Here is some code that does what you want with comments to explain how it works:
function tablelength(table) -- to calculate the length of a table
local count = 0
for _ in pairs(table) do count = count + 1 end -- loop through each of its items and add to a counter
return count
end
function stringtoletters(text) -- split a string into its individual characters
local letters = {}
for a = 1, string.len(text) do -- loop through each character in the string and add it to the table
table.insert(letters, string.sub(text, a, a))
end
return letters
end
function randomlyreorganizetext(text) -- the function that actually does the stuff AKA the meat
local letters = stringtoletters(text)
local n = tablelength(letters)
math.randomseed(os.time()) -- initialize the random number generator
math.random(); math.random(); math.random() -- the first few numbers are apparently not very random so get rid of them
for i=n,1,-1 do -- go through each character and switch it out with another one of the of the characters at random
local j = math.floor(math.random() * (i-1)) + 1 -- decide which character to swap with
local tmp = letters[i] -- make a backup of the current value
letters[i] = letters[j] -- change to random characters value
letters[j] = tmp -- put what used to be the current value into the random characters position
end
return table.concat(letters, " ") -- turn the table back into a string and put spaces between each element
end
print(randomlyreorganizetext("hello"))

Raphael Facredyn
- 146
- 8
-
3Call `math.randomseed` only once in your program, not in a library function. – lhf Aug 08 '18 at 21:46
-
Thanks man that's cool works like magic :) except there is a problem with Arabic letters does not work Can you help me with that – Skora Aug 08 '18 at 22:28
-
1@Destroyer you should probably post another question because I don't know Lua and I think that is a problem with Unicode stuff and I don't know anything about that either. – Raphael Facredyn Aug 09 '18 at 00:42
-
@RaphaelFacredyn Thanks man yes because of the arabic letters string of length 2 , Thank you for everything and for your time very grateful to you ♥ – Skora Aug 09 '18 at 01:08
2
math.randomseed(os.time())
local function shuffle(str)
local letters = {}
for letter in str:gmatch'.[\128-\191]*' do
table.insert(letters, {letter = letter, rnd = math.random()})
end
table.sort(letters, function(a, b) return a.rnd < b.rnd end)
for i, v in ipairs(letters) do letters[i] = v.letter end
return table.concat(letters)
end
print(shuffle("How to shuffle the whole UTF-8 string"))

Egor Skriptunoff
- 906
- 1
- 8
- 23
1
Here is my take. The first gsub
indexes the letters in the string in the order they appear in it. Then we shuffle the indices and use the shuffled indices to remap the letters with another gsub
.
s="How to shuffle the letters of a word using lua"
t={}
n=0
s:gsub("%a",function (c)
if t[c]==nil then
n=n+1
t[n]=c
t[c]=n
end
end)
math.randomseed(os.time())
for i=n,2,-1 do
local j=math.random(i)
t[i],t[j]=t[j],t[i]
end
z=s:gsub("%a",function (c) return t[t[c]] end)
print(s)
print(z)

lhf
- 70,581
- 9
- 108
- 149
-
Now it works But not with arabic words and also there is no spaces between letters – Skora Aug 09 '18 at 01:55
-
1@Destroyer, for adding spaces, use `z=s:gsub("%a",function (c) return t[t[c]].." " end)` – lhf Aug 09 '18 at 12:21