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)
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.
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
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)
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).
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
This kinda works… but do you know how to print the whole table contents?
all it prints is one random letter then errors:
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
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.)
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