Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to print a random string from a table?

Asked by
Ap_inity 112
7 years ago

Hello ScriptingHelpers community! Back again with a question.

How would i pick a random string from a table with strings? I've got a line here, edit as you'd like.

Strings = {
    "line1"
    "line2"
    "line3"
}

for i, v in pairs(Strings) do
    print(math.random(1, v)) -- "1" takes 1 string from the table and "v" prints the string selected
end

Please fix my script if it is not correct, thanks!

3 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

A mistake that I have caught is that you did not put a comma(,) or a semicolon(;) after each string. That is very important to have so you could access those strings. Another thing you must know is that if you put # before a table name, that gets the number of keys (in this case strings) in your table. The final thing you should know that if you want to get the strings in your table or access anything in the table, you would have to do Strings[Which string you want to get]. You have 3 strings, so you are able to put in between the brackets 1, 2 or 3. Here is your fixed script.

local Strings = {"line1", "line2","line3"}
local selectedIndex = math.random(1,#Strings)
local selectedString = Strings[selectedIndex]
print('Selected String is ' .. selectedString)
print('Selected Index is ' .. selectedIndex)

If you wanted to make things truly random, you would put math.randomseed(tick()) on the top of your script.

math.randomseed(tick())
local Strings = {"line1", "line2","line3"}
local selectedIndex = math.random(1,#Strings)
local selectedString = Strings[selectedIndex]
print('Selected String is ' .. selectedString)
print('Selected Index is ' .. selectedIndex)

Please accept my answer if it fixed your script. If not, please ask questions in the comments.

0
Again, i did paste in your 2nd block of code into my script, it only prints one line and is not random. Ap_inity 112 — 7y
0
What do you mean one line, as in one string saSlol2436 716 — 7y
0
Yes, one string. Ap_inity 112 — 7y
0
you want it to print everything in the table saSlol2436 716 — 7y
View all comments (8 more)
0
oh saSlol2436 716 — 7y
0
i understand what you mean saSlol2436 716 — 7y
0
fixed saSlol2436 716 — 7y
0
That still prints the same line over again. Ap_inity 112 — 7y
0
I don't know how to fix that anymore saSlol2436 716 — 7y
0
Randomseed does not work saSlol2436 716 — 7y
0
I don't know actually how to make it truly random anymore saSlol2436 716 — 7y
0
I don't know, either. Ap_inity 112 — 7y
Ad
Log in to vote
0
Answered by
iddash 45
7 years ago

You dont need to user a for i,v in pairs loop for this.

strings = {"line1","line2","line3"} --our strings
print(strings[math.random(3)]) --indexes the table with a random number from 1 to 3, and prints it
0
Would've been better if you used randomseed, so then it actually is random. awfulszn 394 — 7y
0
It does print from the list but it's not random. I've tried "randomseed", but it returns "nil". Ap_inity 112 — 7y
Log in to vote
0
Answered by
H4X0MSYT 536 Moderation Voter
7 years ago

iddash did a great job explaining the MAIN part. math.random will often not call completely random numbers. The first number will allways be the same. You should use randomseeds to randomize the PATTERN of which they are generated.

0
Should've been a comment awfulszn 394 — 7y
0
Downvotes just for that? Wow. H4X0MSYT 536 — 7y
0
Plus, I added an explanation on it. H4X0MSYT 536 — 7y

Answer this question