Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Any way to generate random letters?

Asked by 8 years ago

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.

2 answers

Log in to vote
2
Answered by
Legojoker 345 Moderation Voter
8 years ago
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.

1
This is a very inefficient method since you have to perform a concatenation every iteration. See table.concat(). User#6546 35 — 8y
0
+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. Legojoker 345 — 8y
0
doesnt really have much difference from mine.. lol TheDeadlyPanther 2460 — 8y
0
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) BlueTaslem 18071 — 8y
Ad
Log in to vote
-1
Answered by 8 years ago
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.

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(). Legojoker 345 — 8y

Answer this question