Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to generate random letters?

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

✓ Accepted answer

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

5 comments

0

Nice, I was about to suggest a more primitive method. Although, this is much cleaner. New to me! Thanks

0

How would I make this generate like seven letters instead of one?

0

Create a loop that goes through 7 different times. And store the results in a table so you can refer to them later

0

Edited and added a function to generate a sequence of random numbers

0

Thanks

This discussion is preserved as it was. New answers, comments, and votes are closed.