Last active
June 30, 2024 02:30
-
-
Save haggen/2fd643ea9a261fea2094 to your computer and use it in GitHub Desktop.
Random strings in Lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
math.randomseed(os.time()) | |
local charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" | |
function string.random(length) | |
if length > 0 then | |
return string.random(length - 1) .. charset:sub(math.random(1, #charset), 1) | |
else | |
return "" | |
end | |
end |
pseudo random, but super fast:
for i = 1, 10 do
print( tostring({}):sub(10) )
end
40ae13c8
406c0640
41954060
41a91220
40ae1ac8
4195b718
41958260
41a3ea10
41a3acf0
41a3a978
local sets = {{97, 122}, {65, 90}, {48, 57}} -- a-z, A-Z, 0-9
local function string_random(chars)
local str = ""
for i = 1, chars do
math.randomseed(os.clock() ^ 5)
local charset = sets[ math.random(1, #sets) ]
str = str .. string.char(math.random(charset[1], charset[2]))
end
return str
end
abc = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","r","s","t","u","v","z","y","w","q","1","2","3","4","5","6","7","8","9","0"}
local string = ""
function GeneratedString()
for i=1,35 do
rdm = math.random(1,35)
string = string..abc[rdm]
end
return string
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A small improvement over simplicity and speed