How do I go about generating random letters/numbers like its a code?

I wanna do a math.random that combines 5 letters / numbers to make for a code
for example: 845fb

2 Likes

Probably the simplest generator is the one that chooses (pseudo)random element from a pool of characters.

local characters = "0123456789abcdefghijklmnopqrstuvwxyz"

local RandomGenerator = Random.new()

local function GenerateRandomCode(length: number): string
	local list = {}
	for i=1, length do
		local random = RandomGenerator:NextInteger(1, #characters)
		table.insert(list, characters:sub(random, random))
	end
	return table.concat(list)
end

local code = GenerateRandomCode(5)
print(code)

Keep in mind that these numbers are not truly random, but I haven’t seen the sequence being guessed without knowing the seed yet. Using Random instead of math.random should increase the pseudorandomness.

Also, why didn’t I use the .. concatenator? Because it wastes more resources and memory, creating new strings with each iteration.

3 Likes

For extra randomness and a lower chance of a code being guessed, you can add special characters like @, $, &, etc.

1 Like

That’s right, the more characters there are in the pool, the more combinations we can form.

Then the formula for all possible combinations (where the chars can repeat) is

number of possible characters ^ code length

Edit. Accidentally switched base and exponent in a rush.

Just found out that using a 5 character code with those special characters give us 1,818,989,400,000,000,000,000,000,000 combinations, thats pretty cool.

one octillion eight hundred eighteen septillion nine hundred eighty-nine sextillion four hundred quintillion

So I would suggest that OP uses extra characters if he wants the code to really be a secret.

You can use string.char which uses ASCII codes of characters.

function generateCode()
local code = “”

for i = 1, 5 do
    -- randomly choose between a letter or a number
    local randomType = math.random(1, 2)
    
    if randomType == 1 then
               local randomLetter = string.char(math.random(65, 90)) -- ASCII codes for uppercase letters are 65 to 90

        code = code .. randomLetter
    else
        local randomNumber = tostring(math.random(0, 9))
        code = code .. randomNumber
    end
end

return code

end

Yeah, I was in a hurry at the time, the formula is the other way around. Sorry :blush:

image

These are variations with repetition. n-elements are arranged in k-places, while every element can repeat. Essentially five boxes, and each of these boxes can contain any of the available characters.

ASCII has 128 code points, of which 95 are printable.

95 x 95 x 95 x 95 x 95 = 7,737,809,375



So, 5-character passwords alone are very weak and might take an average computer no more than a few moments to break. At least they are extremely weak in terms of time it would take to brute force them.

This of course depends on whether there is an effective time limit between tries etc. Regular iPhone passwords only consist of 4 or 6 numbers, but the phone quickly locks and erases itself after enough wrong attempts.

When it comes to encryption, for instance disk encryption, let’s say AES-256, password isn’t a cryptographic key. There are various steps of transforming a password into the key, including hashing. Not to get off topic, poor password is usually the weakest link in the chain.

Incrase the password length to 14 or 15, and it might take the same compuer thousands of years to brute force. Add around 5 to those and it’s hundreds of billions of years. :dizzy:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.