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 9 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
9 years ago
1function doText(numLetters)
2    local totTxt = ""
3    for i = 1,numLetters do
4        totTxt = totTxt..string.char(math.random(97,122))
5    end
6    print(totTxt)
7end
8 
9doText(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 — 9y
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 — 9y
0
doesnt really have much difference from mine.. lol TheDeadlyPanther 2460 — 9y
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 — 9y
Ad
Log in to vote
-1
Answered by 9 years ago
01local letters = {"a","b","c","d"} -- etc.
02local length = 5
03local text = ""
04 
05for i = 1,length do
06    text = text.. "" ..letters[math.random(1,#letters)]
07    if i == length then
08        print(text)
09    end
10end

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 — 9y

Answer this question