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

Is it possible to generate a random string of numbers letters and symbols?

Asked by 5 years ago

I know I can generate a string of random numbers, but is it possible to gernerate a random string of letters and symbols and numbers? And if so, how would I do that?

0
string.char is a possibility theking48989987 2147 — 5y
0
maybe you can make a table with all the characters you want, and set the index value to random Fad99 286 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Create your own.

local upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
local lowerCase = "abcdefghijklmnopqrstuvwxyz"
local numbers = "0123456789"
local symbols = "!@#$%&()*+-,./\:;<=>?^[]{}"

local characterSet = upperCase .. lowerCase .. numbers .. symbols

local keyLength = 32
local output = ""

for i = 1, keyLength do
    local rand = math.random(#characterSet)
    output = output .. string.sub(characterSet, rand, rand)
end

print(output)
--Output: nz28IJ..=uPBF}6xeVhOQ7J,LR)CGF46
--Output: 4h<UH%!l.21,;LbG$S2:Ss!Vl>ks{IRJ

Keeping each set separate (lower case, upper case, numbers, etc) makes it easy to dynamically select what sets you want to use. The loop simply runs and selects a random character position and appends it to the final string.

Do note that the quotes, single and double, are left out. You can use the delimiter to add those back in, just be sure to compensate for the length and check for the character(s).

Ad

Answer this question