I was wondering if there is any way to generate random letters, kind of like math.random(1,5) except for letters. Any help appreciated.
✓ ANSWER ACCEPTED
Any way to generate random letters?
2 answers
✓ Accepted answer
function doText(numLetters)
local totTxt = ""
for i = 1,numLetters do
totTxt = totTxt..string.char(math.random(97,122))
end
print(totTxt)
end
doText(12) -- Prints a string of a random 12 letters, this number can be changed; this is one of my outputs with 12: 'avbaajquwzgj'
If you're curious, 65 to 90 happens to be the alphabet capitalized, while 97 to 122 is lowercase.
local letters = {"a","b","c","d"} -- etc.
local length = 5
local text = ""
for i = 1,length do
text = text.. "" ..letters[math.random(1,#letters)]
if i == length then
print(text)
end
end
This prints a 5 randomly generated letters.
1 comment
0
This is a very inefficient method since you have to print out all the letters of the alphabet into that table. See string.char().
This discussion is preserved as it was. New answers, comments, and votes are closed.

4 comments
This is a very inefficient method since you have to perform a concatenation every iteration. See table.concat().
+1 point for the satire, but since I'd be adding the strings to a table anyway through every for loop to concat it at the end, it doesn't actually affect the efficiency. Still upvoting because I like satire.
doesnt really have much difference from mine.. lol
table.concat will actually be faster for very large input, but for this application there's not going to be big numbers (and concatenation's not that slow anyway)