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

How to generate random letters?

Asked by 8 years ago

I was wondering if there is any-way to generate random letter's, like math.random() but for letters. If there is, how would I do it? Any help appreciated.

Sorry I can't really explain any better.

1 answer

Log in to vote
5
Answered by 8 years ago

heres a quick little method using string.char and ASCII characters

local rLetter = string.char(math.random(65,  90)) --gets a random upper-case letter  (65 - 90 are letters in ASCII)
rLetter = math.random() > .5 and rLetter:lower() or rLetter -- 50% chance of making it lowercased

print(rLetter) --random letter :D

And to generate a sequence of random numbers you can use this function:

function randomLetter()
     local rLetter = string.char(math.random(65,  90))
     rLetter = math.random() > .5 and rLetter:lower() or rLetter
     return rLetter
end

function generateRandomSequence(length)
     local sequence = ""
     for i = 1, length do
          sequence = sequence..randomLetter()
     end
     return sequence
end


local sevenNumberSequence = generateRandomSequence(7)
print(sevenNumberSequence) -- BOOM
0
Nice, I was about to suggest a more primitive method. Although, this is much cleaner. New to me! Thanks Necrorave 560 — 8y
0
How would I make this generate like seven letters instead of one? ISellCows 2 — 8y
0
Create a loop that goes through 7 different times. And store the results in a table so you can refer to them later Necrorave 560 — 8y
0
Edited and added a function to generate a sequence of random numbers GrayTide 0 — 8y
0
Thanks ISellCows 2 — 8y
Ad

Answer this question