Skip to content

Instantly share code, notes, and snippets.

@haggen
Last active February 21, 2024 04:16
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save haggen/2fd643ea9a261fea2094 to your computer and use it in GitHub Desktop.
Save haggen/2fd643ea9a261fea2094 to your computer and use it in GitHub Desktop.
Random strings in Lua
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
@brianmhunt
Copy link

One may prefer math.randomseed(os.clock()^5) for better entropy.

@geekhunger
Copy link

geekhunger commented Feb 2, 2018

thank you very much. I shortened it a little

local charset = {}  do -- [0-9a-zA-Z]
    for c = 48, 57  do table.insert(charset, string.char(c)) end
    for c = 65, 90  do table.insert(charset, string.char(c)) end
    for c = 97, 122 do table.insert(charset, string.char(c)) end
end

local function randomString(length)
    if not length or length <= 0 then return '' end
    math.randomseed(os.clock()^5)
    return randomString(length - 1) .. charset[math.random(1, #charset)]
end

@andrewpisula
Copy link

I made my own, its shorter and does the same thing:

function RandomVariable(length)
	local res = ""
	for i = 1, length do
		res = res .. string.char(math.random(97, 122))
	end
	return res
end

@HypheX
Copy link

HypheX commented Nov 16, 2018

Oh, mysterious, you should set the random seed, it generates the same hash every time...

@datavisorchengpeng
Copy link

datavisorchengpeng commented Aug 4, 2021

The calling stack would be too deep if length is great enough.
Be cautious abt the danger of stack overflow.

@mysterious4579 's solution might be better.
Thanks anyway.

@Perthys
Copy link

Perthys commented Aug 25, 2021

math.randomseed(os.time())
local character_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

local string_sub = string.sub
local math_random = math.random
local table_concat = table.concat
local character_set_amount = #character_set
local number_one = 1
local default_length = 10

local function generate_key(length)
    local random_string = {}

    for int = number_one, length or default_length do
        local random_number = math_random(number_one, character_set_amount)
        local character = string_sub(character_set, random_number, random_number)

        random_string[#random_string + number_one] = character
    end

    return table_concat(random_string)
end




@Minishapp
Copy link

math.randomseed(os.time())
local character_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

local string_sub = string.sub
local math_random = math.random
local table_concat = table.concat
local character_set_amount = #character_set
local number_one = 1
local default_length = 10

local function generate_key(length)
    local random_string = {}

    for int = number_one, length or default_length do
        local random_number = math_random(number_one, character_set_amount)
        local character = string_sub(character_set, random_number, random_number)

        random_string[#random_string + number_one] = character
    end

    return table_concat(random_string)
end

thank you psu man

@GreedyTactician
Copy link

local charset = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890"
math.randomseed(os.clock())
function randomString(length)
	local ret = {}
	local r
	for i = 1, length do
		r = math.random(1, #charset)
		table.insert(ret, charset:sub(r, r))
	end
	return table.concat(ret)
end

A small improvement over simplicity and speed

@AMD-NICK
Copy link

AMD-NICK commented Mar 9, 2023

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

@AMD-NICK
Copy link

AMD-NICK commented Mar 9, 2023

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

@wavesk
Copy link

wavesk commented Jul 31, 2023

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