如何让 Lua 中的字符串包含随机字符?

How do i make a string in Lua contain random characters?

我很好奇如何将随机字符串打印到 Lua 中的输出 我想知道这是否可能用于字符串。因为我知道您可以使用调用的 Lua 中的函数生成随机数。 math.Random() 但我不确定如何使字符串随机。我怎样才能使字符串中的字符随机打印到输出?

-- I want to print out random characters in a string to the consoles output
local Number = math.random(1,80) -- prints out a number 1-80
print(Number)

大写字母在string.char(math.random(65, 90))中,可以用另一种字符串方法懒惰地降低...

local randuppercase = string.char(math.random(65, 65 + 25))
local randlowercase = string.char(math.random(65, 65 + 25)):lower()

print(randlowercase, randuppercase)
-- Example output: g    W

没有字符串方法 lower() 小写字母开始于...

local randlowercase = string.char(math.random(97, 97 + 25))

print(randlowercase, randlowercase:upper())
-- Sample output: c    C

另一种可能的解决方法是将它做得更老派...

local chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" -- The Char Library
local rint = math.random(1, #chars) -- 1 out of length of chars
local rchar = chars:sub(rint, rint) -- Pick it

print(rint, rchar)
-- Sample Output: 12    L