Random Tables

Learn how to use "random" on a table.

by KyleDominicus

Author Avatar

A "table" can be simple expressed as an algebraic table.

One side it shows the place of the value / string, and the other size

is what it is equalled to.

Here is an example of a table:

local woodenTable = {3,4,2,6}
-- The number "3" is the first digit, the number "4" i the second digit,
-- the number "2" is the third digit, and the number "6" is the fourth.

Here is an example of how you can print the part of the table you want:


This will allow me to print the first digit, the "3", into the output.

But, finding a random digit in the table is a little bit harder.


Instead of typing a digit into between the square brackets, I instead replaced

it with a math.random function.

The "1" between the brackets (for the math.random), represents the minimum,

or in this case, it just lets me find a random in all of the table.

The "#woodenTable" is the maximum, or in this case, what the script is limited to

choosing, which is whatever is in the table. The "#" means "number", and it will

change the "woodenTable" variable into the amount of digits within the table. So, with the "#woodenTable", the script is able to only choose digits that are

within the table. In my case, I have only 4 digits to choose from: 3,4,2, and 6.

View in-game to comment, award, and more!