Answers for "random element in table lua"

Lua
1

how to choose a random item from a table lua

local myTable = {"Hello", "World", "Lua"}

print(myTable[1]) --Outputs: "Hello"
print(#myTable) --Outputs: 3 because we have 3 values in our table
print(myTable[math.random(1, #myTable)]) --Outputs a random value in our table!
Posted by: Guest on December-29-2020

Browse Popular Code Answers by Language