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

How to pick a random string from a table?

Asked by 8 years ago

I was wondering how could I get a random string for a table, for example

local table = {"Player1", "Player2", "Player3"}

-- Get random string from table
print(randomstring)

How could I get it to pick 1 out of the 3 strings in the table?

3 answers

Log in to vote
3
Answered by 8 years ago

math.random

What you're looking for is the math.random function in the math library. This function has a very simple mechanic to it; you input a number, and it outputs a random number given the range. We can utilize this randomly generated number in many ways, and in this case, solve the problem of receiving a random value from your array.

Arguments

In case this wasn't clear from the introduction above, math.random only deals with numbers. Meaning the argument we must input, has to be a number. The amount of arguments you give this function will effect the outcome in different ways. Here are some examples:

1: Using a single argument

By returning a single number to this function, it will generate a random number between 1, and whatever number you gave it. Like this:

print(math.random(10)) -- > Random number between 1 and 10

2: Using 2 arguments

Or, we could create our own range of numbers by providing the function with a second argument. This new range will return a random number between the first argument, and the second. Like this:

print(math.random(10,20)) -- > Random number between 10 and 20

3: Using no arguments

It's important to note that math.random only returns whole numbers. Even if you provide a number with a decimal. The only way you can get a random decimal number, is by calling math.random() with no arguments. Which will return a random decimal between 0 and 1. Here's an example:

print(math.random()) -- > Some decimal between 0 and 1

If you wanted a decimal number larger than 1, you could simply multiply the result:

print(math.random()*100) -- > Random decimal number between 0 and 100

Getting a random value

Now for the actual part of your question, getting the random value. Well, this should be pretty simple at this point if you know how to index an array-styled table. We can simply use brackets with the given random number on the table to return the random value, like this:

local Table = {"Player1","Player2","Player3"}
local Rand = math.random(#Table) -- Note that I'm using #Table, which will return the number 3, since we have 3 values in your array. This is key for assurance that all possible values from the table can be evaluated.

print(Table[Rand]) -- > Random value from the table

Or to make things quicker, we could do this:

local Table = {"Player1","Player2","Player3"}

print(Table[math.random(#Table)]) 
-- In this example we just index the table directly. Either solution will work the same.

Non-array styled tables?

If you've noticed from above, we can easily return the amount of content in an array by using the # symbol before it. However, this will not work for dictionary-styled tables. A dictionary styled table is an array, with defined keys. Here's an example:

-- A dictionary styled table in which 'x', 'y', and 'z' are manually defined keys.
local Dictionary = {x = 1, y = 2, z = 3}

-- Since we've defined these keys, we don't need to index this table with a number. We can index it with our defined keys, like this:

print(Dictionary.x) -- > 1

-- Or...

-- Using this method gives us flexibility to give our keys special characters that regular variables can't work with (i.e, spaces, symbols, ect.)
print(Dictionary["x"]) -- > 1

-- Array styled table in which our keys are automatically set to a number that represents it's position in the table. (i.e, 1 = 'x', 2 = 'y', 3 = 'z')
local Array = {'x','y','z'}

-- Which is why when we index arrays, we always use a number to do so:
print(Array[1]) -- > 'x'
print(Array[2]) -- > 'y'
print(Array[3]) -- > 'z' -- Or print(Array[#Array]) -- > 'z'

Problem

However, this will not work for dictionary styled tables. Since we've overwritten all the numeric keys (in the case of a dictionary table), our ability to sense how much data is in the table is gone. We would have to find a way to convert our dictionary table to an array, and then index it with a random number. Here's an example:

local Dictionary = {x = 1,y = 2,z = 3}
print(#Dictionary) -- > prints 0

local function ConvertDictionaryToArray(d)
    local Array = {} -- Create a new table
    for i,v in next, d do -- Iterate the dictionary
        Array[#Array+1] = v -- This is basically table.insert. It just assigns a new index that's one above the last value, inserting a value at the very end of an array.
    end
    return Array -- Return the table to the function that called it
end

local ArrayVersion = ConvertDictionaryToArray(Dictionary)
print(#ArrayVersion) -- > prints 3

print(ArrayVersion[math.random(#ArrayVersion)]) -- > Random value from the array

Hope this helped.

Ad
Log in to vote
1
Answered by 8 years ago
local table = {"Player1", "Player2", "Player3"}

local function RandomString(t)
    return t[math.random(1, #t)]
end

print(RandomString(table))
Log in to vote
0
Answered by
QsDoes 0
5 years ago

local Strings = {"Player1", "Player2"} local st = String[math.random(1, #Strings)] print(st)

Answer this question