Trying to print Letters from a Table by Random

Im trying to retrieve 10 different Letters from alphabet randomly. Then im trying to print the 10 random letters im getting but i dont know how to do that.


--																								--
--										THE ALPHABET										--		
--																								--
		local alphabet	= {	A = "A";
							B = "B";
							C = "C";
							D = "D";
							E = "E";
							F = "F";
							G = "G";
							H = "H";
							I = "I";
							J = "J";
							K = "K";
							L = "L";
							M = "M";
							N = "N";
							O = "O";
							P = "P";
							Q = "Q";
							R = "R";
							S = "S";
							T = "T";
							U = "U";
							V = "V";
							W = "W";
							X = "X";
							Y = "Y";
							Z = "Z"}
--																							    --
--------------------------------------------------------------------------------------------------
game.Players.PlayerAdded:Connect(function(player)
	
local plrData = Instance.new("Folder")
plrData.Name = 'Data'
plrData.Parent = player

local plrID = Instance.new("StringValue")
plrID.Name = player.Name.."'S ID"
plrID.Parent = plrData
plrID.Value = ""

local RandomAlphabet = math.random(10,#alphabet)
if plrID.Value == nil then
	print(RandomAlphabet)																					
	 end
 end)
3 Likes

So a good shortcut is knowing A-Z are actually in order in the ASCII table.

As such, you can do something relatively simple to obtain any random letter through them without a huge lookup table.

local my_letter = string.char(math.random(string.byte('A'), string.byte('Z')))

Breaking it down, what this means is you pick a random number with math.random, which is between the ASCII codes of the letters A and Z, and then you use string.char to turn that number back into a character.

If you need to generate longer strings of letters, you can put this in a loop to fit your needs.

3 Likes

Well i need to generate random letters from A to Z 10 times.

Then like I said at the end, you could use a loop. This makes an empty table to store your characters in, and iterates 10 times adding each random character to the array. At the end, table.concat is called to concatenate the entire table in 1 go into a string. While this could also be achieved by repeatedly concatenating using the .. operator, it’s generally bad practice to do so in Lua.

local my_string = {}

for i = 1, 10 do
    my_string[i] = string.char(math.random(string.byte('A'), string.byte('Z')))
end

my_string = table.concat(my_string)

Maybe add in a lil’ randomseed in there for maximum randomness :wink:

local my_string = {}

math.randomseed(os.time())

for i = 1, 10 do
    my_string[i] = string.char(math.random(string.byte('A'), string.byte('Z')))
end

my_string = table.concat(my_string)
1 Like

do i need to make the random letter be inserted into the table my_string to see if it works?
would it be like:
table.insert(my_string,my_string[i])?

That’s what’s essentially happening in the for loop; the table is getting a randomized string from A-Z 10 times, and each value is concatenated (added together) to form a single string (using table.concat).

yes but how do i print whats in the table or how does the code enter a letter 10 times?

Im not familiar with string.char,string.byte and table.concat

Each letter of the alphabet is assigned a certain number (or byte). For this case:

string.byte("A") == 65
string.byte("B") == 66
string.byte("C") == 67
...
string.byte("Z")  == 90

so when you see math.random(string.byte('A'), string.byte('Z')), it really is just

math.random(65,90)
-- picks a number between 65 and 90 (or A to Z)

Now, what string.char does is it changes that “number” back into a character.
For example: string.byte("A") = 65 so similarily, string.char(65) will get the character, which is "A".

So, overall, what’s happening in that one line is:

string.char(math.random(string.byte('A'), string.byte('Z')))

= string.char(math.random(65, 90)) -- turns the int value into a string value

= String Value (like "A", "C", or "Z") etc.

And we will have 10 Values like this in our table:

my_string = {"A", "J", "K" ... } -- 10 random letters

-- when we use table.concat

my_string = table.concat(my_string)
 -- turns everything in the table together into a string

print(my_string)
Output -> "AJKLMEGFDS" -- basically a set of random letters
1 Like

This kinda works… but do you know how to print the whole table contents?
all it prints is one random letter then errors:
g

 local my_string = {}

if plrID.Value == "0" then
	for i = 1, 10 do
	my_string[i] = string.char(math.random(string.byte('A'), string.byte('Z')))
	my_string = table.concat(my_string)
	print(my_string)
	wait(1)
end

Use table.concat after the for loop, so you get the whole set of characters before you concatenate them together.

local my_string = {}

if plrID.Value == "0" then
	for i = 1, 10 do
	   my_string[i] = string.char(math.random(string.byte('A'), string.byte('Z')))
    end
    my_string = table.concat(my_string)
    print(my_string)
end

Ok thank you, that worked but could i get 10 random numbers from 1,9 10 times by doing this?

my_stringID[i] = string.char(math.random(1,9))

Im trying to make a unique Player ID per-person.

Example: 1X0QT145IPMH395AZXWS

Overwriting a variable with a different type is dirty. There’s really no need to use a table anyway.

local randomString = ""
for i = 1, 10 do
	randomString = randomString..string.char(math.random(65, 90))
end
print(randomString)

another approach is to use multiple arguments in string.char

local randomChars = {}
for i = 1, 10 do
	randomChars[i] = math.random(65, 90)
end
print(string.char(table.unpack(randomChars)))

Yep, you don’t need the string.char either.

my_stringID[i] = math.random(1,9)

well the numbers dont print at all with the random letters. Because after it prints it will be a value for someones profile in-game.

It will be for a StringValue

God your guys code is so over complicated…

just do

for 1,10 do
    print(table[math.random(1,#table)]) -- Table would hold all the letters.
end

That is what OP uses essentially, but it’s a waste of memory and can be easily solved by using several methods from the string library. (I mean he uses a dictionary but it’s still the same idea.)

1 Like

He does not tho, he uses a more lengthy version. Sure you can use string. But this is the easiest. Also could you explain how its a waste of memory.

The code in the OP doesn’t work or make sense. The concept is to make a table with all of the letters in it. This is unnecessary because you can get random letters without making a table filled with letters.

For random capital letters and numbers, since their char values are not consecutive, a reasonable option in this case is to make a table and fill it with the set of characters that are being selected from.

local characters = {}
for i = 65, 90 do
	characters[#characters+1] = string.char(i)
end
for i = 0, 9 do
	characters[#characters+1] = tostring(i)
end

local function randomString()
	local result = ""
	for i = 1, 10 do
		result = result..characters[math.random(#characters)]
	end
	return result
end
5 Likes

Would this help to print the Letters and Number in one line, together.?