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.
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.
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
This discussion is preserved as it was. New answers, comments, and votes are closed.
5 comments
Nice, I was about to suggest a more primitive method. Although, this is much cleaner. New to me! Thanks
How would I make this generate like seven letters instead of one?
Create a loop that goes through 7 different times. And store the results in a table so you can refer to them later
Edited and added a function to generate a sequence of random numbers
Thanks